视频不会与2个媒体播放器实例一起显示

时间:2012-03-20 21:14:00

标签: android android-mediaplayer

我正在尝试使用两个mediaplayer实例。播放第一个实例时,第二个实例在另一个线程中准备。当第一个实例完成文件时,第二个实例就会启动。

问题:第二个实例只播放音频,视频不可见。

观察:如果我使用与第一个实例相同的表面支架准备第二个实例并在第一个实例完成后启动它可以正常工作。 这是代码

package com.THER;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.THER.MessengerServiceActivities.IncomingHandler;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.VideoView;
import android.widget.ViewSwitcher;

public class MediaPlayerActivity extends Activity implements OnCompletionListener, Callback, OnPreparedListener {

private MediaPlayer mMediaPlayer1 = null;
    private SurfaceView mPreview1;
    private SurfaceHolder holder1;
    private ViewSwitcher switcher;

    Integer counter = 0;
    Integer played_files = 1;
    private MediaPlayer mMediaPlayer2 = null;
    private SurfaceView mPreview2;
    private SurfaceHolder holder2;
    boolean mIsBound;
    boolean finish_flag = false;
    /** Messenger for communicating with service. */
    Messenger mService = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Intent intent; 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoview);
        doBindService();
        intent = getIntent();
        counter=intent.getIntExtra("counter",counter);


        mPreview1 =(SurfaceView)findViewById(R.id.surfaceView1);
        holder1 = mPreview1.getHolder();
        holder1.addCallback(this);  
        holder1.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mPreview2 =(SurfaceView)findViewById(R.id.surfaceView2);
        holder2 = mPreview2.getHolder();
        holder2.addCallback(this);  
        holder2.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mMediaPlayer1 = new MediaPlayer();
        mMediaPlayer1.setScreenOnWhilePlaying(true);
        mMediaPlayer2 = new MediaPlayer();
        mMediaPlayer2.setScreenOnWhilePlaying(true);
        try {

                mMediaPlayer1.setDataSource("/sdcard/1.mp4");
                mMediaPlayer1.setDisplay(holder1);

            } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                e.printStackTrace();
            }



        mMediaPlayer1.setOnCompletionListener(this);
        mMediaPlayer1.setOnPreparedListener(this);
        //mMediaPlayer2.setOnPreparedListener(this);
        mMediaPlayer2.setOnCompletionListener(this);




        }
    private class MediaPlayerTask extends AsyncTask<MediaPlayer, Void, String> implements OnCompletionListener {

            String file;
            private SurfaceHolder holder;
        @Override
            protected String doInBackground(MediaPlayer... mp1) {
                // TODO Auto-generated method stub
             //mp1[0].reset();
             if(counter%2 == 0)
             {
                file = "/sdcard/2.mp4";
                holder = holder2;
             }
             else
             {
                file = "/sdcard/1.mp4";
                holder = holder1;
             }

            try {

                    mp1[0].reset();
                    mp1[0].setDataSource(file);
                    mp1[0].setDisplay(holder);
                    mp1[0].prepare();
                    mp1[0].setOnCompletionListener(this);
                    //mp1[0].start();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
            }
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub

        }


}



public void onCompletion(MediaPlayer mp)
{

     if(counter%2 == 0)
        {
            played_files++;
            mMediaPlayer1.stop();
            mMediaPlayer1.release();

            mMediaPlayer2.start();


        }
        else
        {
            played_files++;
            mMediaPlayer2.stop();
            mMediaPlayer2.release();

            mMediaPlayer1.start();  

        }
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    try {
        if(counter%2 == 0)
        {
            mMediaPlayer2.prepare();
        }
        else
        {
            mMediaPlayer1.prepare();
        }
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

public void onPrepared(MediaPlayer mp) {
    // TODO Auto-generated method stub
    //if(counter == 1)
        mp.start();

}

}

1 个答案:

答案 0 :(得分:1)

请查看以下主题:  http://groups.google.com/group/android-developers/browse_thread/thread/7d08a124a2e48bb3?pli=1

Android开发人员并不打算使用两个SurfaceView。