Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 474
Quamber Ali Avatar asked Jul 22 '13 07:07

Quamber Ali


People also ask

Which of the following method of the MediaPlayer class uses the SurfaceHolder object to display video content?

The Media Player requires a SurfaceHolder object for displaying video content, assigned using the setDisplay() method. The Surface View is a wrapper around the Surface Holder object. Note that we must implement the SurfaceHoler. Callback interface.

What is MediaPlayer release?

android.media.MediaPlayer. MediaPlayer class can be used to control playback of audio/video files and streams. MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.

How do I reset my MediaPlayer on Android?

Just use pause to stop, followed by seekTo(0) before restarting: mediaPlayer. seekTo(0); mediaPlayer.

What is MediaPlayer in Android explain transitions through the state machine?

Android is providing MediaPlayer class to access built-in mediaplayer services like playing audio,video e.t.c. In order to use MediaPlayer, we have to call a static Method create() of this class. This method returns an instance of MediaPlayer class. Its syntax is as follows − MediaPlayer mediaPlayer = MediaPlayer.


2 Answers

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.
like image 100
gvmani Avatar answered Oct 05 '22 00:10

gvmani


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.

like image 42
David Avatar answered Oct 05 '22 01:10

David