.pause()不适用于多个MediaPlayer

时间:2014-07-12 13:17:51

标签: android android-mediaplayer onpause

我有多个MediaPlayers,我想使用独立按钮暂停它们。我对使用单个MediaPlayer的另一个应用程序没有任何问题,但现在方法“.pause”给我带来了问题。

在日志中可以看到方法“.isPlaying”工作正常,但视频没有暂停。我不知道我做错了什么。

当我第一次点击按钮时,暂停,我获取此日志:

07-12 15:08:59.786: V/MediaPlayer-JNI(1562): isPlaying: 1
07-12 15:08:59.786: I/MediaPlayer(1562): sendBroadcast SMART_PAUSE_stop()
07-12 15:08:59.786: V/MediaPlayer-JNI(1562): stop
07-12 15:08:59.786: V/MediaPlayer(1562): stop
07-12 15:08:59.786: V/MediaPlayer(1562): message received msg=7, ext1=0, ext2=0
07-12 15:08:59.786: V/MediaPlayer(1562): unrecognized message: (7, 0, 0)
07-12 15:08:59.786: V/MediaPlayer(1562): callback application
07-12 15:08:59.786: V/MediaPlayer(1562): back from callback
07-12 15:08:59.826: V/MediaPlayer-JNI(1562): getCurrentPosition: 5080 (msec)
07-12 15:08:59.826: V/MediaPlayer-JNI(1562): isPlaying: 0

第二次,恢复,我获得此日志:

07-12 15:08:24.501: V/MediaPlayer-JNI(1562): isPlaying: 0
07-12 15:08:24.506: I/MediaPlayer(1562): sendBroadcast SMART_PAUSE_start()
07-12 15:08:24.506: W/MediaPlayer(1562): this is IMEDIA_PLAYER_VIDEO_EXIST
07-12 15:08:24.506: I/MediaPlayer(1562): sendBroadcast android.media.IMediaPlayer.videoexist
07-12 15:08:24.506: V/MediaPlayer-JNI(1562): start
07-12 15:08:24.506: V/MediaPlayer(1562): start
07-12 15:08:24.506: E/MediaPlayer(1562): start called in state 64
07-12 15:08:24.506: V/MediaPlayer(1562): message received msg=100, ext1=-38, ext2=0
07-12 15:08:24.506: E/MediaPlayer(1562): error (-38, 0)
07-12 15:08:24.506: V/MediaPlayer(1562): callback application
07-12 15:08:24.506: V/MediaPlayer(1562): back from callback
07-12 15:08:24.506: E/MediaPlayer(1562): Error (-38,0)
07-12 15:08:24.506: D/MediaPlayer(1562): MediaPlayer(0): onCompletion called

可能是什么问题?

我的代码是:

public class Reproductor extends Activity implements
    OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
    OnVideoSizeChangedListener, SurfaceHolder.Callback, View.OnClickListener {

private Bundle extras;
private static final String PATH = "path";
private static final String PATH2 = "path2";
private static final String TAG = "MediaPlayer";
private int[] SURFACE_RES_IDS = { 
    R.id.video_1_surfaceview, R.id.video_2_surfaceview};

private MediaPlayer[] mMediaPlayers = new MediaPlayer[SURFACE_RES_IDS.length];
private SurfaceView[] mSurfaceViews = new SurfaceView[SURFACE_RES_IDS.length];
private SurfaceHolder[] mSurfaceHolders = new SurfaceHolder[SURFACE_RES_IDS.length];
private boolean[] mSizeKnown = new boolean[SURFACE_RES_IDS.length];
private boolean[] mVideoReady = new boolean[SURFACE_RES_IDS.length];
private int mVideoWidth;
private int mVideoHeight;
private Button bPlayPause1, bPlayPause2, bMute1, bMute2;


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.repr_multiple);
    extras = getIntent().getExtras();
    mVideoWidth=1200;
    mVideoHeight=900;
    bPlayPause1 = (Button)findViewById(R.id.bPlayPause1);
    bPlayPause2 = (Button)findViewById(R.id.bPlayPause2);
    bMute1 = (Button)findViewById(R.id.bMute1);
    bMute2 = (Button)findViewById(R.id.bMute2);

    bPlayPause1.setOnClickListener(this);
    bPlayPause2.setOnClickListener(this);
    bMute1.setOnClickListener(this);
    bMute2.setOnClickListener(this);

    // create surface holders
     for (int i=0; i<mSurfaceViews.length; i++) {
        mSurfaceViews[i] = (SurfaceView) findViewById(SURFACE_RES_IDS[i]);
        mSurfaceHolders[i] = mSurfaceViews[i].getHolder();
        mSurfaceHolders[i].addCallback(this);
        mSurfaceHolders[i].setFixedSize(mVideoWidth, mVideoHeight);
    }
}

public void onBufferingUpdate(MediaPlayer player, int percent) {
    Log.d(TAG, "MediaPlayer(" + indexOf(player)
            + "): onBufferingUpdate percent: " + percent);
}

public void onCompletion(MediaPlayer player) {
    Log.d(TAG, "MediaPlayer(" + indexOf(player) + "): onCompletion called");
}

public void onVideoSizeChanged(MediaPlayer player, int width, int height) {
    Log.v(TAG, "MediaPlayer(" + indexOf(player)
            + "): onVideoSizeChanged called");
    if (width == 0 || height == 0) {
        Log.e(TAG, "invalid video width(" + width + ") or height(" + height
                + ")");
        return;
    }

    int index = indexOf(player);
    if (index == -1)
        return; // sanity check; should never happen
    mSizeKnown[index] = true;

    if (mVideoReady[index] && mSizeKnown[index]) {
        startVideoPlayback(player);
    }
}

public void onPrepared(MediaPlayer player) {
    Log.d(TAG, "MediaPlayer(" + indexOf(player) + "): onPrepared called");

    int index = indexOf(player);
    if (index == -1)
        return; // sanity check; should never happen
    mVideoReady[index] = true;
    startVideoPlayback(player);
    /*
    if (mVideoReady[index] && mSizeKnown[index]) {
        startVideoPlayback(player);
    }
    */
}

public void surfaceChanged(SurfaceHolder holder, int i, int j, int k) {
    Log.d(TAG, "SurfaceHolder(" + indexOf(holder)
            + "): surfaceChanged called");
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "SurfaceHolder(" + indexOf(holder)
            + "): surfaceDestroyed called");
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "SurfaceHolder(" + indexOf(holder)
            + "): surfaceCreated called");

    int index = indexOf(holder);
    if (index == -1)
        return; // sanity check; should never happen
    try {
        mMediaPlayers[index] = new MediaPlayer();
        if(index == 0){
            mMediaPlayers[index].setDataSource(extras.getString(PATH));
        }else if(index == 1){
            mMediaPlayers[index].setDataSource(extras.getString(PATH2));
        }else{
            mMediaPlayers[index].setDataSource(extras.getString(PATH2));
        }
        mMediaPlayers[index].setDisplay(mSurfaceHolders[index]);
        mMediaPlayers[index].setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayers[index].prepareAsync();
        mMediaPlayers[index].setOnBufferingUpdateListener(this);
        mMediaPlayers[index].setOnCompletionListener(this);
        mMediaPlayers[index].setOnPreparedListener(this);
        mMediaPlayers[index].setOnVideoSizeChangedListener(this);


    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    super.onPause();
  //  releaseMediaPlayers();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayers();
}

private void releaseMediaPlayers() {
    for (int i = 0; i < mMediaPlayers.length; i++) {
        if (mMediaPlayers[i] != null) {
            mMediaPlayers[i].release();
            mMediaPlayers[i] = null;
        }
    }
}

private void startVideoPlayback(MediaPlayer player) {
    Log.v(TAG, "MediaPlayer(" + indexOf(player) + "): startVideoPlayback");
    player.start();
}

private int indexOf(MediaPlayer player) {
    for (int i = 0; i < mMediaPlayers.length; i++)
        if (mMediaPlayers[i] == player)
            return i;
    return -1;
}

private int indexOf(SurfaceHolder holder) {
    for (int i = 0; i < mSurfaceHolders.length; i++)
        if (mSurfaceHolders[i] == holder)
            return i;
    return -1;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bPlayPause1:
        if(mMediaPlayers[0].isPlaying()){
            mMediaPlayers[0].pause();  
            bPlayPause1.setText("Resume");
        }else{
            mMediaPlayers[0].start();
            bPlayPause1.setText("Pause");
        }                                               
        break;
    case R.id.bPlayPause2:
        if(mMediaPlayers[1].isPlaying()){
            mMediaPlayers[1].pause();
            bPlayPause2.setText("Resume");
        }else{
            mMediaPlayers[1].start();
            bPlayPause2.setText("Pause");
        }
        break;
    case R.id.bMute1:
        break;
    case R.id.bMute2:
        break;          
    }
  }
}

0 个答案:

没有答案