Android 4.0.4 MediaPlayer prepare issue using RTSP urls
Asked Answered
C

1

8

I am experiencing an odd issue with a video streaming application I am working on. The actual streaming of video/audio is working fine on all of my test devices. However, on seemingly any device 4.0+, when using an RTSP URL, prepare() returns instantly (this causes an issue providing proper feedback to the users while a video is loading and interferes with a few other systems I have in place).

Below is the block of code where I initialize and setup my MediaPlayer, but keep a few things in mind:

  • My initPlayer method is called from an AsyncTask.
  • The video does eventually play correctly, but prepare returning instantly creates a lack of feedback to the user during a video load.
  • No errors of any kind occur during the entire process
  • start() is called on the MediaPlayer via the onPrepared method in my OnPreparedListener, which obviously becomes an issue when prepare() returns before it is actually ready to be played.
  • HTTP streams seem to work fine, and on every test device below 4.0 the issue does not occur.

I have been trying to fix this for a ridiculous amount of time, and haven't been able to find anyone else who has ran into this problem. Any ideas would be greatly appreciated.

    public void initPlayer() {
        //We first need to make sure the MediaPlayer isn't null
        if(mMediaPlayer==null){
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnPreparedListener(mediaPlayerPreparedListener);
            mMediaPlayer.setOnCompletionListener(mediaPlayerCompletionListener);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
        //If a video/stream has been chosen while another is already playing
        else if(mMediaPlayer.isPlaying()){
            mMediaPlayer.reset();
        }
        //Video is not in full screen mode
        second = false;
        try {
            mMediaPlayer.setDataSource(videoString);
            holder = mPreview.getHolder();
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //onPreparedListener
    private OnPreparedListener mediaPlayerPreparedListener = new OnPreparedListener(){
        public void onPrepared(MediaPlayer mp) {
            mp.start();
            vidPb.setVisibility(View.INVISIBLE);
        }
    };
Cymbal answered 21/9, 2012 at 20:52 Comment(2)
i am facing the same issue right now. Have you resolved the Issue.? if yes please Answer you own question with the solution. waiting...!!!Morrow
Unfortunately I never found a solution, although I stopped working on this project quite a while ago.Cymbal
B
0

Use mp.prepareAsync() as it is better for streaming media. Using prepare() blocks until MediaPlayer is ready for playback or an IllegalStateException occurs. Also, in android 4 (ICS), blocking on any UI thread is even more strict and may cause an ANR (Activity not responding) dialog to appear.

One final thought, try to avoid using e.printStackTrace(); in android apps. Instead, use the Log.e("TAG_STRING", e.getMessage(), e); to print errors to the android logging system that you can access from logcat.

All in all, it should looks something like this:

    try {
        mMediaPlayer.setDataSource(videoString);
        holder = mPreview.getHolder();
        mMediaPlayer.setDisplay(holder);
        mMediaPlayer.prepareAsync();
    } catch (IllegalArgumentException e) {
        Log.e("TAG_STRING", e.getMessage(), e);
    } catch (SecurityException e) {
        Log.e("TAG_STRING", e.getMessage(), e);
    } catch (IllegalStateException e) {
        Log.e("TAG_STRING", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("TAG_STRING", e.getMessage(), e);
    }
Bimetallism answered 6/9, 2013 at 14:52 Comment(1)
Thanks petey. I am calling prepare from an AsyncTask, so it is not blocking the UI thread. Regardless, I have tried using prepareAsync() as well with the same results.Cymbal

© 2022 - 2024 — McMap. All rights reserved.