可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to show user some ProgressDialog while he waits for VideoView to start play HLS. I try to use OnPreparedListener for this, but he triggers to early (after player downloaded m3u8 file, not when video started).
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();
I didn't find another suitable listner... May be anyone knows sloution?
回答1:
Thanks to all. I solved my problem with next 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); } });
回答2:
I experienced the same issue, and just found a solution for my needs. Maybe this works for you, too. At least it's tested & works on Android 2.2, 2.3, and 4.2.
The idea is to periodically check if the videoView current position is greater than zero. It's a modification of mikhail's answer. Thanks to mikhail, too :)
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); } }