IllegalStateException for MediaPlayer.prepareAsync

后端 未结 5 1373
一向
一向 2020-12-28 12:23
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
相关标签:
5条回答
  • 2020-12-28 12:54

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

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

    0 讨论(0)
  • 2020-12-28 12:54

    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.

    0 讨论(0)
  • 2020-12-28 13:01

    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.

    0 讨论(0)
  • 2020-12-28 13:06

    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();
    
    0 讨论(0)
  • 2020-12-28 13:09

    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).

    0 讨论(0)
提交回复
热议问题