gpt4 book ai didi

android - videoView.setOnPreparedListener、videoView.setOnCompletionListener 和 videoView.setOnErrorListener 未被调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:15 29 4
gpt4 key购买 nike

这是我的代码 fragment ,我想在其中播放来自服务器的视频

private void PlayVideo() {
try {
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();

videoPlayer.removeAllViews();
videoPlayer.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {
dismissProgressDialog();
videoView.bringToFront();
videoView.setFocusable(true);
videoView.start();
contentStarted = true;
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
contentStarted = false;
}
});
videoView.setOnErrorListener(new OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
return false;
}

});

} catch (Exception e) {
dismissProgressDialog();
finish();
}
}

ProgressDialog 仅在 videoView.setOnPreparedListenervideoView.setOnErrorListener 中被关闭。但是 ProgressDialog 没有被关闭,视频也没有播放。我尝试放置日志并查看,日志打印到 videoView.setOnPreparedListener 之前,之后不显示任何日志。我猜 Listeners 没有注册。

感谢任何帮助。提前致谢。

编辑:

'我正在尝试流式传输实时视频,如果视频可用,它应该转到 videoView.setOnPreparedListener 并播放视频。如果直播不可用(即,视频将在一段时间后直播),那么它应该转到 videoView.setOnErrorListener 并返回到先前的 Activity,结果为“1”

视频通过 RTSP 流式传输

最佳答案

经过大量研究并在 Preethi Rao 的帮助下,我了解到错误出在 URL 中。

URL 可能有视频(如果是实时视频的时间),也可能没有视频(如果实时视频将在一段时间后流式传输)。如果我在实时视频不可用时尝试流式传输视频,则 Listeners 不会被触发。如果实时视频可用,Listeners 将被解雇。因此,videoView.setOnPreparedListenervideoView.setOnErrorListener 没有附加。

我编写了一个运行 60 秒的处理程序,如果没有附加 Listeners,我将返回到之前的 Activity

代码如下:

private void PlayVideo() {
try {
isListenerAttached = false;
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(VideoActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videoPath);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();

videoPlayer.removeAllViews();
videoPlayer.setVisibility(View.GONE);
videoView.setVisibility(View.VISIBLE);

// Using this Handler to revert to previous Activity when the Video View is not attached to Listeners
// As the Buffering Video dialog doesn't get dismissed if Video View is not attached to Listeners
runOnUiThread(new Runnable() {

@Override
public void run() {
new Handler().postDelayed(new Runnable(){

@Override
public void run() {
if(!isListenerAttached){
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
}
}
}, WAIT_TIME);
}
});

videoView.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {
isListenerAttached = true;
dismissProgressDialog();
videoView.bringToFront();
videoView.setFocusable(true);
videoView.start();
contentStarted = true;
}
});
videoView.setOnCompletionListener(new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
contentStarted = false;
}
});
videoView.setOnErrorListener(new OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
isListenerAttached = true;
dismissProgressDialog();
Intent in = new Intent();
setResult(1, in);
finish();
return false;
}

});

} catch (Exception e) {
dismissProgressDialog();
finish();
}
}

关于android - videoView.setOnPreparedListener、videoView.setOnCompletionListener 和 videoView.setOnErrorListener 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30340700/

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