【问题标题】:VideoView triggers OnPreparedListener too early for HLSVideoView 为 HLS 过早触发 OnPreparedListener
【发布时间】:2012-11-29 16:02:00
【问题描述】:

我想在用户等待 VideoView 开始播放 HLS 时向他展示一些 ProgressDialog。我尝试为此使用 OnPreparedListener,但他会提前触发(在播放器下载 m3u8 文件后,而不是在视频开始时)。

VideoView player = (VideoView) findViewById(R.id.player);
String httpLiveUrl = "http://example.com/playlist.m3u8";

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

player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
    progress.dismiss();
}
});

progress.show();
player.start();

我没有找到另一个合适的监听器...可能有人知道 sloution 吗?

【问题讨论】:

  • 去 Handler.PostDelayed()

标签: android http-live-streaming


【解决方案1】:

谢谢大家。我用下一个 hack 解决了我的问题:

videoView.setOnPreparedListener(new OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {

        final Handler handler = new Handler();
        videoPositionThread = new Runnable() {
             public void run() {
                try {
                    int duration = videoView.getCurrentPosition();
                    if (!(videoPosition == duration && videoView.isPlaying())) {
                        progress.dismiss();
                    }
                    videoPosition = duration;
                    handler.postDelayed(videoPositionThread, 1000);
                } catch (IllegalArgumentException e) {
                    Log.d(TAG, e.getMessage(), e);
                }
            }
        };
        handler.postDelayed(videoPositionThread, 0);
    }
});

【讨论】:

  • 你能帮帮我吗?什么是 videoPosition?
  • 我不知道为什么我的 videoview 在延迟超过 15 或 20 秒后显示实时视频流。
【解决方案2】:

我遇到了同样的问题,只是找到了满足我需求的解决方案。也许这也适合你。至少它在 Android 2.2、2.3 和 4.2 上已经过测试和运行。

这个想法是定期检查 videoView 当前位置是否大于零。这是对米哈伊尔答案的修改。也感谢米哈伊尔 :)

public class VideoViewActivity extends Activity {

    // Declare variables
    ProgressDialog pDialog;
    VideoView videoview;
    Runnable videoPositionThread;

    // Insert your Video URL
    String VideoURL = "enter your video rtsp url here";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoview_main);
        videoview = (VideoView) findViewById(R.id.VideoView);

        pDialog = new ProgressDialog(VideoViewActivity.this);
        pDialog.setTitle("Android Video Streaming Tutorial");
        pDialog.setMessage("Buffering...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

        Uri video = Uri.parse(VideoURL);
        videoview.setVideoURI(video);
        videoview.start();

        final Handler handler = new Handler();
        videoPositionThread = new Runnable() {
            public void run() {
                int currentPosition = videoview.getCurrentPosition();

                if (currentPosition > 0)
                    pDialog.dismiss();
                else
                    handler.postDelayed(videoPositionThread, 1000);
            }
        };

        handler.postDelayed(videoPositionThread, 0);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2017-11-01
    • 1970-01-01
    相关资源
    最近更新 更多