简体   繁体   中英

Android - MediaPlayer's on Prepare Called even before the stream is prepared on Android 4.0+

I am facing the issue that whenever a stream is played by my app on Android 4.0+ the OnPrepare method from MediaPlayer.OnPreparedListener is called even before a stream is loaded and thus i am unable to indicated the user that the stream downloading/buffering is in process. I have already found a question of the same kind but not answered Here is a what i am doing.

   @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        playVideo(someRtspUrl);
    }
    private void playVideo(String url) {
        // if app is running on Google TV then change RTSP link to HLS
        if (url.contains("rtsp")) {

            // split the RTSP URL and make it as HLS
            String videoUrlParts[] = url.split("\\?");
            url = videoUrlParts[0].replace("rtsp", "http") + "/playlist.m3u8";

            if (videoUrlParts.length > 1)
                url += "?" + videoUrlParts[1];
        }   mVideoView.setVideoURI(Uri.parse(url));
        mVideoView.requestFocus();
        mVideoView.setOnCompletionListener(this);
        mVideoView.setOnPreparedListener(this); 

    }



    @Override
        public void onPrepared(MediaPlayer player) {
            dismissProgressDialog();
            mVideoView.start();
        }

This Code is Working fine on Google TV and other Android 3.0+ and < 4.0+ device

I don't have much experience with media player. But couple of suggestions/queries from my side

  1. No call to prepare. If you are doing it, did you try prepareAsync ?
  2. You are not using the mediaplayer instance which is passed to the onPrepared callback. There can be a chance that you are trying to start the wrong mediaplayer.

I would suggest using the MediaPlayer class that is available. Unlike the VideoView MediaPlayer is aware of its state and manages even more things for you that VideoView doesn't offer.. Paired with setting up a SurfaceHolder to display the content in it is pretty simple.

You'll just want to make sure that you are properly handling the state and using the prepareAsync() call of MediaPlayer.

This is a known issue for .m3u8 with Android < 4.3.

They fixed this in 4.3. It will be worth to compare MediaPlayer or VideoView code of different platforms ie 4.3 & < 4.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM