VideoView为HLS过早触发OnPreparedListener

我想向用户展示一些ProgressDialog,而他等待VideoView开始播放HLS。 我尝试使用OnPreparedListener来做这件事,但是他提前触发(播放器下载了m3u8文件,而不是video开始时)。

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

我没有find另一个合适的listner …可能有谁知道sloution?

Solutions Collecting From Web of "VideoView为HLS过早触发OnPreparedListener"

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

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

我遇到了同样的问题,只是find了我的需求的解决scheme。 也许这也适用于你。 至less它已经过testing,可以在Android 2.2,2.3和4.2上运行。

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

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