gpt4 book ai didi

android - 如何在android中单屏播放多个视频?

转载 作者:行者123 更新时间:2023-11-29 01:45:46 27 4
gpt4 key购买 nike

我正在开发一个与“视频拼贴”相关的应用程序,在此用户可以在单个布局文件中播放多个视频。为此,我使用 Video Views 来播放视频。但问题是用户点击保存按钮布局文件被转换并作为视频文件保存到 SD 卡中。如果用户打开保存的文件,它会播放多个视频。但是我正在使用视频 View 播放视频,但是视频 View 不能有任何缓存方法来保存视频。

任何人都可以告诉我我该怎么做吗?

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

 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();
}
}

最佳答案

下面的代码在一个屏幕上播放两个视频。我使用了 RTSP 链接。你可能获取 Error(1, -110) 这意味着超时错误,如果你的网络不好。 Surface views 被放置在一个线性布局中,给定宽度相等的权重。

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);
}

关于android - 如何在android中单屏播放多个视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21625866/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com