这是我的视频代码
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
SingleItemView.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(Uri.parse(video));
videoview.requestFocus();
videoview.setKeepScreenOn(true);
videoview
.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mediaPlayer) {
mProgressDialog.dismiss();
videoview.start();
}
});
} catch (Exception e) {
mProgressDialog.dismiss();
Log.e("Error", e.getMessage());
e.printStackTrace();
}
但如果视频流无效,则对话框仍然存在。
如果视频无效,我想把对话框放回主要活动..
我怎么能这样做?
我尝试把对话但仍然不工作我不知道为什么..
提前谢谢:)
编辑:我也尝试这样但仍然无法正常工作
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
SingleTrackActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(Uri.parse(duration));
videoview.requestFocus();
videoview.setKeepScreenOn(true);
videoview
.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(
MediaPlayer mediaPlayer) {
pDialog.dismiss();
videoview.start();
}
});
} catch (Exception e) {
AlertDialog alertDialog = new AlertDialog.Builder(
SingleTrackActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Video Play Error :"
+ e.toString());
pDialog.dismiss();
System.out.println("Video Play Error :" + e.toString());
finish();
}
答案 0 :(得分:2)
媒体播放器的工作是从您的代码异步完成的。如果媒体设置失败,则代码完成您的方法后将失败。同时,由于媒体没有进入onPreparedListener
状态,因此未调用PREPARED
。
因此,请尝试添加OnErrorListener
:
videoview.setOnErrorListener(new MediaPlayer.OnErrorListener(){
public boolean onError(MediaPlayer mp, int what, int extra) {
// inform the user something went wrong
mProgressDialog.dismiss();
return true;
}
});
如果这不起作用,请检查您为媒体播放器/视频查看获得的回调。