MediaPlayer.prepareAsync 的 IllegalStateException
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
mp = MediaPlayer. create(...);
已经在准备返回的 MediaPlayer,因此您无法再次调用prepare
(或其变体)(并且也不需要 onPreparedListener )。mp = MediaPlayer.create(...);
is already preparing the MediaPlayer returned, so you cannot callprepare
(or its variants) again (and there is no need for onPreparedListener as well).“prepareAsync 在状态 8 中调用” 表示 Mediaplayer 已准备就绪。
您在代码中调用
mp.prepare();
吗?"prepareAsync called in state 8" means the Mediaplayer is already prepared.
are you calling
mp.prepare();
in your code?您更新的问题:
AndroidManifest.xml
中是否具有 INTERNET 权限 检查onPrepared()
方法。更好的方法是这样写:
Your updated question:
AndroidManifest.xml
onPrepared()
method never to be called.A better approach would be to write:
基本问题在于在“不允许的状态”下调用 MediaPlayer 的方法。状态图此处显示。例如,在没有准备媒体文件的情况下调用
start()
方法是不允许的,并且会抛出异常。由于
MediaPlayer
不公开getState()
方法,因此您应该在外部跟踪状态。示例实现可以此处找到。Base Problem lies on calling methods of
MediaPlayer
at "not allowed states". State Diagram is shown here. For example, callingstart()
method without preparing the media file is not allowed and will throw Exception.Since
MediaPlayer
does not exposegetState()
method, you should track states externally. Sample implementation can be found here.我使用下面的代码来播放 http 的声音文件。
它显示警告
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.
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.