10

我想在用户等待 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吗?

4

2 回答 2

15

谢谢大家。我用下一个 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);
    }
});
于 2012-12-12T12:25:56.187 回答
1

我遇到了同样的问题,只是找到了满足我需求的解决方案。也许这也适合你。至少它已经过测试并适用于 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);
    }
}
于 2014-10-18T08:47:48.743 回答