Android MediaPlayer not returning from prepareAsyn

2019-07-27 07:03发布

问题:

I'm getting the following back in Logcat starting MediaPlayer with a SPECIFIC URI. Normally every Uri, good or bad, will either play or come back with an error except this particular one.

I/MPS﹕ PrepAsync started
V/MediaPlayer﹕ message received msg=8, ext1=0, ext2=0
V/MediaPlayer﹕ unrecognized message: (8, 0, 0)
V/MediaPlayer﹕ callback application
V/MediaPlayer﹕ back from callback

... and hangs there.

I'm really just looking at how do I capture this conversation in an error handler but if someone knows the actual problem that's even better.

Source code FWIW:

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            mediaPlayer.start();
        }
    });
}

try {
    mediaPlayer.setDataSource(sUrl);
    mediaPlayer.prepareAsync();
} catch (Exception e) {
    Log.d(TAG, "Exception:"+e;
}

Also, I've tried creating an OnInfoListener, OnError and OnBufferingUpdateListener. These are never called. It's seems that mediaPlayer just goes away during onPrepareAsync.

Here is the URL if anyone is inspired to play with this.

http://54.196.206.122/entercom-koitfmaac-64

I just copy/pasted this into VLC to verify that the link is valid.

UPDATE: After looking at it more, if I wait long enough, eventually I get this:

I/dalvikvm﹕ threadid=3: reacting to signal 3
I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'

UPDATE: This problem as was pointed out by Shubhang Malviya was that I needed to use URI.parse as:

mMediaPlayer.setDataSource(mContext, Uri.parse("http://54.196.206.122/entercom-koitfmaac-64"));

回答1:

I thought It would be good If I share my implementation:

Try the following way of initialising your media player

try {
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mMediaPlayer.setOnPreparedListener(this);
                    mMediaPlayer.setOnCompletionListener(this);
                    mMediaPlayer.setOnErrorListener(this);
                    mMediaPlayer.setOnBufferingUpdateListener(this);
                    mMediaPlayer.setOnInfoListener(this);
                    mMediaPlayer.setOnSeekCompleteListener(this);
                    mMediaPlayer.setDataSource(mContext, Uri.parse("http://54.196.206.122/entercom-koitfmaac-64"));
                    mMediaPlayer.prepareAsync();
                } catch (IOException e) {
                 // reset media player
                }

Mine onPrepared(MediaPlayer mp) is getting called after only a few seconds and it is playing your Music File.

Also FYI "Couldn't open file on client side, trying server side" is not an error message, but a debug message from the MediaPlayer. Logcat always says this when trying to play a network video stream.