Android - VideoView MediaController 无法正常工作
Android - VideoView with MediaController don't work correctly
我需要创建 Activity,它将播放来自 URL 的视频。
现在,我使用 VideoView 和 MediaController,当这个 activity 创建时,在缓冲视频后它应该播放。它可以工作,但只播放一次,之后媒体控制器可以工作但不播放视频。
这是一段代码:
private ProgressDialog progressDialog;
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Some title");
progressDialog.setMessage("Buffering...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
videoView = (VideoView) findViewById(R.id.video_view);
Intent intent = getIntent();
String url = intent.getStringExtra("extra_url");
Uri uri = Uri.parse(url);
videoView.setVideoURI(uri, HeadersMap); // Here I put my headers - I need to do that.
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
}
});
}
那么,有什么问题吗?为什么视频不能重复?我将光标设置为在控制器上启动,但没有任何反应。我该如何处理?或者我应该怎么做?
感谢您的解答!
试试这个,
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
你也可以用 setOnCompletionListener
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
videoView.start();
}
});
或
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
mVideoView.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
videoView.setVideoURI(uri, HeadersMap);
}
});
我需要创建 Activity,它将播放来自 URL 的视频。 现在,我使用 VideoView 和 MediaController,当这个 activity 创建时,在缓冲视频后它应该播放。它可以工作,但只播放一次,之后媒体控制器可以工作但不播放视频。
这是一段代码:
private ProgressDialog progressDialog;
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Some title");
progressDialog.setMessage("Buffering...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
videoView = (VideoView) findViewById(R.id.video_view);
Intent intent = getIntent();
String url = intent.getStringExtra("extra_url");
Uri uri = Uri.parse(url);
videoView.setVideoURI(uri, HeadersMap); // Here I put my headers - I need to do that.
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
}
});
}
那么,有什么问题吗?为什么视频不能重复?我将光标设置为在控制器上启动,但没有任何反应。我该如何处理?或者我应该怎么做?
感谢您的解答!
试试这个,
videoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
你也可以用 setOnCompletionListener
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
videoView.start();
}
});
或
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
mVideoView.start();
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
videoView.setVideoURI(uri, HeadersMap);
}
});