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

交替

如果我做:

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

我得到:

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
4

5 回答 5

97

mp = MediaPlayer.create(...);已经在准备返回的 MediaPlayer,因此您不能prepare再次调用(或其变体)(也不需要 onPreparedListener)。

于 2011-05-19T16:20:38.713 回答
10

“在状态 8 中调用了prepareAsync”表示 Mediaplayer 已经准备好了。

mp.prepare();在调用你的代码吗?

于 2011-05-19T16:18:41.607 回答
8

您更新的问题:

  1. 检查您是否有 INTERNET 权限AndroidManifest.xml
  2. 检查您是否启用了某些数据连接,因为您想从互联网流式传输
  3. “这个解决方案也失败了”是什么意思?它会抛出 IllegalStateException 吗?据我所知,它根本不会做任何事情,因为您在 MediaPlayer 对象准备好自己之后注册了 OnPreparedListener,从而导致该onPrepared()方法永远不会被调用。

更好的方法是编写:

MediaPlayer mp = new MediaPlayer();  
mp.setDataSource("http://.../movie.mp4");  
mp.setOnPreparedListener(this);  
mp.prepareAsync();
于 2012-01-25T15:27:34.903 回答
0

我使用下面的代码来播放 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;
    }
}

它显示警告W/MediaPlayer: Couldn't open https://something.com/test.mp3: java.io.FileNotFoundException: No content provider: https://something.com/test.mp3但工作正常。

于 2016-12-07T05:35:42.633 回答
0

基本问题在于调用MediaPlayer“不允许状态”的方法。状态图显示在这里。例如,不准备start()媒体文件就调用方法是不允许的,会抛出异常。

由于MediaPlayer不公开getState()方法,因此您应该在外部跟踪状态。示例实现可以在这里找到

于 2019-07-24T12:01:13.677 回答