Android 4.0.4 MediaPlayer prepare issue using RTSP urls

做~自己de王妃 提交于 2019-12-13 12:04:27

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/12538013/android-4-0-4-mediaplayer-prepare-issue-using-rtsp-urls

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