我正在运行一个在线流,实现示例代码:

VideoView videoView = (VideoView) findViewById(R.id.videoView);
    String httpLiveUrl = "......";

    videoView.setVideoURI(Uri.parse(httpLiveUrl));
    videoView.setMediaController(new MediaController(this));

    videoView.requestFocus();
    videoView.start();


加载活动后,就会出现黑屏,并在一段时间后运行视频。正如我在文档中阅读的那样,它需要在不同于UI线程的环境下运行。但是,当我添加run()时,视频根本无法启动。这里的方法是什么?

最佳答案

我认为这不是需要线程处理的代码问题。

the activity is loaded, a black screen appears and after a while the video is run.


此观察结果表明视频正在播放,但仅在适当缓冲了足够的内容以开始播放之后。

您可以做的是,通过在当前布局中显示/隐藏您选择的图像并调用VideoView#setOnPreparedListener,向应用程序用户指示视频正在缓冲中

这是一个例子:

VideoView videoView = (VideoView) findViewById(R.id.videoView);
String httpLiveUrl = "......";

videoView.setVideoURI(Uri.parse(httpLiveUrl));
videoView.setMediaController(new MediaController(this));
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        // media file is loaded and ready to go.
        hideBufferingUi();
    }
});

videoView.requestFocus();
showBufferingUi();
videoView.start();


剩下要做的就是


选择一张图片
将其添加到您的布局
添加showBufferingUihideBufferingUi方法


hths!

10-04 23:00
查看更多