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

3

6

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

Mimimimic answered 22/7, 2013 at 7:46 Comment(0)
A
1

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.
Amorous answered 31/7, 2013 at 13:30 Comment(1)
Thanks for the Suggestions/Queries @gvmani. for the second one i would say that i am playing the orignal/main object. for the 1st one. prepare synchronously is also working. the issue is on specific OSMimimimic
P
1

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.

Polyandrist answered 2/8, 2013 at 19:12 Comment(1)
well the issue is that the If Video View is unaware of its state then how it is working fine on Android 3.1 and above.Mimimimic
F
1

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 i.e 4.3 & < 4.3

Friedafriedberg answered 2/8, 2013 at 20:0 Comment(1)
unfortunately this is not the case .. i i have checked rtsp and other streams too. and for the less than 4.3 case it works on 3.0 and above uptil before 4.0.4 as mentioned in the questionMimimimic

© 2022 - 2024 — McMap. All rights reserved.