如何在Android的单个屏幕上播放多个视频?

时间:2014-02-07 10:54:23

标签: android layout android-videoview

我正在开发与“视频拼贴”相关的应用,在此用户可以在单个布局文件中播放多个视频。为此,我使用视频观看播放视频。但问题是用户点击保存按钮,布局文件被转换并保存为视频文件到SD卡。如果用户打开已保存的文件,则会播放多个视频。但我使用视频视频播放视频,但视频视图无法使用任何缓存方法来保存视频。

是谁可以让我知道我该怎么做?

我正在使用以下代码来播放多个视频。

 public class MainActivity extends Activity {

 String SrcPath = "sdcard/dhoo3.mp4";
 String SrcPath2 = "sdcard/DCIM/Camera/20140206_124925.mp4";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
    //myVideoView.setVideoURI(Uri.parse(SrcPath));
   myVideoView.setVideoPath(SrcPath);
  //  MediaController mc=new MediaController(getApplicationContext());
   // myVideoView.setMediaController(new MediaController(this));
   // mc.setAnchorView(myVideoView);
    //mc.setMediaPlayer(myVideoView);
    //myVideoView.setMediaController(mc);

    myVideoView.setOnPreparedListener(new OnPreparedListener() {

        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            myVideoView.start();
            myVideoView.requestFocus();
            mp.start();


        }
    });


   // myVideoView.start();

    final VideoView myVideoView2 = (VideoView)findViewById(R.id.myvideoview2);
    //myVideoView2.setVideoURI(Uri.parse(SrcPath2));
    myVideoView2.setVideoPath(SrcPath2);
   // myVideoView2.setMediaController(new MediaController(this));
    myVideoView2.setOnPreparedListener(new OnPreparedListener() {

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

             myVideoView2.start();
             myVideoView2.requestFocus();
            mp.start();
            mp.setVolume(0f, 0f);

        }
    });
   // myVideoView2.start();
}
}

1 个答案:

答案 0 :(得分:1)

以下代码在单个屏幕上播放两个视频。我使用了RTSP链接。你可能会 如果您的网络不正确,则获取错误(1,-110),这意味着超时错误。曲面视图以线性布局放置,给定的宽度相等。

public class MainActivity extends Activity {

private MediaPlayer mPlayer1;
private MediaPlayer mPlayer2;

SurfaceHolder.Callback mCallback1;
SurfaceHolder.Callback mCallback2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPlayer1 = new MediaPlayer();
    mPlayer2 = new MediaPlayer();

    mCallback1 = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            try {
                mPlayer1.setDisplay(surfaceHolder);
                mPlayer1.setDataSource(MainActivity.this, Uri.parse("http://media.rc.rit.edu/grav-screenshots/grav264.video.mp4-ipad.video.mp4"));
                mPlayer1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mPlayer1.start();
                    }
                });
                mPlayer1.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            mPlayer1.release();
        }
    };

    mCallback2 = new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            try {
                mPlayer2.setDisplay(surfaceHolder);
                mPlayer2.setDataSource(MainActivity.this, Uri.parse("http://media.rc.rit.edu/grav-screenshots/grav264.video.mp4-ipad.video.mp4"));
                mPlayer2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        mPlayer2.start();
                    }
                });
                mPlayer2.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            mPlayer2.release();
        }
    };

    final SurfaceView surfaceView1 =
            (SurfaceView)findViewById(R.id.surfaceView1);
    // Configure the Surface View.
    surfaceView1.setKeepScreenOn(true);
    // Configure the Surface Holder and register the callback.
    SurfaceHolder holder1 = surfaceView1.getHolder();
    holder1.addCallback(mCallback1);
    holder1.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    final SurfaceView surfaceView2 =
            (SurfaceView)findViewById(R.id.surfaceView2);
    // Configure the Surface View.
    surfaceView2.setKeepScreenOn(true);
    // Configure the Surface Holder and register the callback.
    SurfaceHolder holder2 = surfaceView2.getHolder();
    holder2.addCallback(mCallback2);
    holder2.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

}