对于HLS,VideoView过早触发OnPreparedListener



我想在用户等待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();

我没有找到另一个合适的听者...也许有人知道斯洛特吗?

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

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);
    }
});

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

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

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);
    }
}

相关内容

  • 没有找到相关文章

最新更新