VideoView triggers OnPreparedListener too early for HLS

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!