IllegalStateException for MediaPlayer.prepareAsync

ぐ巨炮叔叔 提交于 2019-12-18 11:41:58

问题


05-19 11:52:51.622: ERROR/MediaPlayer(1291): prepareAsync called in state 8
05-19 11:52:51.622: WARN/System.err(1291): java.lang.IllegalStateException
try {
    mp = MediaPlayer.create(
            Main.this,
            Uri.parse("http://codejanitor.us/good.mp3"));
    mp.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
    try {
        mp.prepareAsync();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }
} finally {
    if (mp != null) {
        mp.release();
        mp = null;
    }
}

ALTERNATELY

If I do:

try {
    mp = MediaPlayer.create(
            AmazonClipActivity.this,
            Uri.parse("http://codejanitor.us/good.mp3"));
    mp.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
} finally {
    if (mp != null) {
        mp.release();
        mp = null;
    }
}

I get:

05-19 12:22:57.472: DEBUG/MediaPlayer(1635): Couldn't open file on client side, trying server side
05-19 12:22:57.472: INFO/StagefrightPlayer(68): setDataSource('http://codejanitor.us/good.mp3')
05-19 12:22:57.482: INFO/NuHTTPDataSource(68): connect to codejanitor.us:80/good.mp3 @0
05-19 12:23:00.632: INFO/NuCachedSource2(68): ERROR_END_OF_STREAM

回答1:


mp = MediaPlayer.create(...); is already preparing the MediaPlayer returned, so you cannot call prepare (or its variants) again (and there is no need for onPreparedListener as well).




回答2:


"prepareAsync called in state 8" means the Mediaplayer is already prepared.

are you calling mp.prepare(); in your code?




回答3:


Your updated question:

  1. Check whether you have INTERNET permission in your AndroidManifest.xml
  2. Check whether you have some data connection enabled, as you want to stream from the internet
  3. What do you mean with "this solution also fails"? Does it throw an IllegalStateException? From what I see, it just won't do anything at all, because you register your OnPreparedListener after the MediaPlayer object has prepared itself, causing the onPrepared() method never to be called.

A better approach would be to write:

MediaPlayer mp = new MediaPlayer();  
mp.setDataSource("http://.../movie.mp4");  
mp.setOnPreparedListener(this);  
mp.prepareAsync();



回答4:


I use below code to play sound files for http.

BackgroundSound mBackgroundSound = new BackgroundSound();

public void onSoundRequested(final Uri uri) {
    mBackgroundSound = new BackgroundSound();
    mBackgroundSound.execute(new SoundModel(dicId, uri));
}

public class BackgroundSound extends AsyncTask<SoundModel, Void, Void> {
    MediaPlayer mediaPlayer;

    @Override
    protected Void doInBackground(SoundModel... params) {
        SoundModel model = params[0];
        final Uri uri = model.getUri();

        if (uri == null || uri == Uri.EMPTY) return null;
        if (mediaPlayer != null) mediaPlayer.stop();

        try {
            mediaPlayer = MediaPlayer.create(VocabularyActivity.this, uri);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        } catch (Exception e) {
            // do nothing.
        }
        if (mediaPlayer == null) return null;

        mediaPlayer.setVolume(1.0f, 1.0f);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            }
        });
        mediaPlayer.start();
        return null;
    }
}

It shows warnimg W/MediaPlayer: Couldn't open https://something.com/test.mp3: java.io.FileNotFoundException: No content provider: https://something.com/test.mp3 but works fine.




回答5:


Base Problem lies on calling methods of MediaPlayer at "not allowed states". State Diagram is shown here. For example, calling start() method without preparing the media file is not allowed and will throw Exception.

Since MediaPlayer does not expose getState() method, you should track states externally. Sample implementation can be found here.



来源:https://stackoverflow.com/questions/6061436/illegalstateexception-for-mediaplayer-prepareasync

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