MediaPlayer.prepareAsync 的 IllegalStateException

发布于 2024-11-08 04:26:30 字数 1498 浏览 1 评论 0原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傻比既视感 2024-11-15 04:26:30

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

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

十年九夏 2024-11-15 04:26:30

“prepareAsync 在状态 8 中调用” 表示 Mediaplayer 已准备就绪。

您在代码中调用 mp.prepare(); 吗?

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

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

奢望 2024-11-15 04:26:30

您更新的问题:

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

更好的方法是这样写:

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

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();
淡写薰衣草的香 2024-11-15 04:26:30

基本问题在于在“不允许的状态”下调用 MediaPlayer 的方法。状态图此处显示。例如,在没有准备媒体文件的情况下调用start()方法是不允许的,并且会抛出异常。

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

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.

不必你懂 2024-11-15 04:26:30

我使用下面的代码来播放 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: 无法打开 https://something.com/test.mp3: java.io.FileNotFoundException: 没有内容提供程序: https://something.com/test.mp3 但工作正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文