MediaPlayer.OnCompletionListener和View.OnClickListener?

时间:2011-03-19 22:21:14

标签: android sdk onclick media-player

我正在尝试:

extends Activity implements MediaPlayer.OnCompletionListener 
extends Activity implements View.OnClickListener 

同时它不起作用或者我不确定如何实现它......我将如何做到这一点?

编辑:如果我向你们展示我现在拥有的东西并且它不起作用,也许会有所帮助:

package com.vamp6x6x6x.rusty;

import java.io.IOException;

import com.vamp6x6x6x.rusty.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class rustyactivity extends Activity implements MediaPlayer.OnCompletionListener, View.OnClickListener {
    /** Called when the activity is first created. */

    ImageView display;
    int toPhone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        display = (ImageView) findViewById(R.id.IVDisplay);
        ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
        ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
        ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
        ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
        ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
         Button setWall = (Button) findViewById(R.id.bSetWallpaper);

        toPhone = R.drawable.guy1;

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        image5.setOnClickListener(this);
        setWall.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.IVimage1:
        display.setImageResource(R.drawable.guy1);
        toPhone = R.drawable.guy1;
        break;
        case R.id.IVimage2:
            display.setImageResource(R.drawable.guy2);
            toPhone = R.drawable.guy2;
            break;
        case R.id.IVimage3:
            display.setImageResource(R.drawable.guy3);
            toPhone = R.drawable.guy3;
            break;
        case R.id.IVimage4:
            display.setImageResource(R.drawable.guy4);
            toPhone = R.drawable.guy4;
            break;
        case R.id.IVimage5:
            display.setImageResource(R.drawable.guy5);
            toPhone = R.drawable.guy5;
            break;

        case R.id.bSetWallpaper:

            Bitmap whatever = BitmapFactory.decodeStream(getResources().openRawResource(toPhone));
               try{
                   getApplicationContext().setWallpaper(whatever);
               }catch(IOException e){
                   e.printStackTrace();
               }
        }

        Button ending = (Button) findViewById(R.id.theme);
        ending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playSound(R.raw.theme);
            }       
 });
    }
        private void playSound(int resId) {
            MediaPlayer mp = MediaPlayer.create(this, resId);
            mp.setOnCompletionListener(this);
            mp.start();
        }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub

    }
    }

3 个答案:

答案 0 :(得分:5)

extends Activity implements MediaPlayer.OnCompletionListener, View.OnClickListener 

然后你需要注册你的活动。

mediaPlayer.setOnCompletionListener(this);
someView.setOnClickListener(this);

'this'就是您刚刚创建的活动

答案 1 :(得分:2)

作为onCreate()

的一部分,在开始时直接粘贴此代码
MediaPlayer mp = MediaPlayer.create(this, pathToTheFile, web, whereverTheSoundIs);
mp.setOnCompletionListener(this);
mp.start();

如果这不起作用,那么您在查找声音文件时遇到问题,或者格式错误。

根据经验,将事情简化为简单的部分,并在继续前进之前尝试让每个部分都先工作。

我做的另一件事就是在每个}之后添加评论,例如:'} // End of Case'

哦,差点忘了,在onCompletion你想用

关闭媒体播放器
mp.release();

干杯

答案 2 :(得分:0)

试试这个!它适用于我,我希望它可以帮助你:
主:

package com.hairdryer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

import com.hairdryer.ServiceReproductor;

public class Reproductor extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageButton btnInicio = (ImageButton) findViewById(R.id.on);
        ImageButton btnFin = (ImageButton) findViewById(R.id.off);
        btnInicio.setOnClickListener(this);
        btnFin.setOnClickListener(this);
    }

    public void onClick(View src) {
        ImageButton btnInicio = (ImageButton) findViewById(R.id.on);

        switch (src.getId()) {
        case R.id.on:
            btnInicio.setBackgroundResource(R.drawable.on2);
            startService(new Intent(this, ServiceReproductor.class));
            break;
        case R.id.off:
            stopService(new Intent(this, ServiceReproductor.class));
            btnInicio.setBackgroundResource(R.drawable.on);
            break;
        }
    }
}

ServiceReproductor.java:

package com.hairdryer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.widget.Toast;


public class ServiceReproductor extends Service implements OnCompletionListener{
    private MediaPlayer player;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        //Toast.makeText(this, "Servicio Creado", Toast.LENGTH_LONG).show();    
        player = MediaPlayer.create(this, R.raw.inicio);
        player.setOnCompletionListener(this);

    }

    @Override
    public void onDestroy() {
        //Toast.makeText(this, "Servicio Detenido", Toast.LENGTH_LONG).show();
        player.stop();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        //Toast.makeText(this, "Servicio Iniciado", Toast.LENGTH_LONG).show();
        player.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        player = MediaPlayer.create(this, R.raw.secador);
        player.start();
        player.setLooping(true);
        // TODO Auto-generated method stub

    }   

}

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/secador"
        />

    <ImageButton    android:id="@+id/off"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/off"

        />
    <ImageButton    android:id="@+id/on"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/off"
        android:layout_marginRight="10dp"
        android:background="@drawable/on"

        />

</RelativeLayout>