MediaPlayer.prepare 在播放 m4a 文件时抛出 IllegalStateException
我有一个使用 MediaPlayer 流式传输的歌曲列表。有些歌曲始终有效,而另一些则始终无效。我看不出这些文件之间有什么区别,而且它们似乎在 iTunes 等中播放得很好。
当歌曲失败时,它会在 mediaPlayer.prepare() 行上抛出 IllegalStateException。抛出的 IllegalStateException 中没有有用的信息,(detailMessage 为 null,stackState 为 null)
这是我的代码
try {
mediaPlayer.setDataSource(media.url);
setPlayerState(PlayerState.PREPARING);
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "bad stream");
}
这是不起作用的文件的 url: skdy.bryceb.dev.mediarain.com/song.m4a
这是一个可行的方法: skdy.bryceb.dev.mediarain.com/song2.m4a
有什么想法为什么这对某些歌曲有效而对其他歌曲无效?
I have a list of songs that I'm streaming using the MediaPlayer. Some of the songs consistently work and others consistently do not work. I can't see a difference between these files, and they seem to play fine in itunes and such.
When the songs fail it is throwing an IllegalStateException on the mediaPlayer.prepare() line. The IllegalStateException that is thrown has no useful info in it, (detailMessage is null, stackState is null)
Here is my code
try {
mediaPlayer.setDataSource(media.url);
setPlayerState(PlayerState.PREPARING);
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "bad stream");
}
Here is a url to the file that does NOT work:
skdy.bryceb.dev.mediarain.com/song.m4a
Here is one that DOES work:
skdy.bryceb.dev.mediarain.com/song2.m4a
Any ideas why this works on some songs and fails on others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢MisterSquonk,我确信这种方法会起作用。
在我的特殊情况下,在用头撞墙一段时间后,我意识到在某些歌曲上,我在播放器状态设置为准备之前就达到了缓冲量。所以我添加了一个检查以确保 MediaPlayer 处于“PREPARED”状态,然后它工作得很好:
Thanks MisterSquonk I'm sure that way would work.
In my particular case after beating my head against the wall for a while I realized that on some songs, I was getting to the buffered amount before the player state was getting set to prepared. So I added a check to make sure that the MediaPlayer was in the "PREPARED" state and then it worked great:
好的,我在“沙盒”应用程序/活动中编写了一个最小的 Mediaplayer 实现,我总是保留备用以进行测试。
我可能是错的,但如果您通过网络流式传输这些歌曲,则需要在 URL 前面加上 http:// 前缀。
我逐字尝试了 Winamp 和 Chrome 的 url(无协议前缀字符串),它们工作得很好,尽管这两个应用程序可能都会使用某种形式的智能来确定如何连接/流式传输。
如果我在 mediaPlayer 代码中尝试这样做,我会得到与您相同的异常,但如果我在 URL 中添加 http:// 前缀,则歌曲可以正常播放。
示例...
如果我将歌曲复制到 SD 卡,它们都可以正常播放,并且只要互联网 URL 字符串具有“http://”前缀,它们也可以正常播放。
OK, I hacked together a minimal Mediaplayer implementation in a 'sandbox' app/activity I always keep spare for testing.
I might be wrong but if you're streaming these songs over the net, you'll need to prefix the url with http://.
I tried the urls with Winamp and Chrome verbatim (no protocol prefix string) and they worked fine although it's likely both of those applications will use some form of intelligence to work out how to connect/stream.
If I tried that in my mediaPlayer code, I get the same exception as you but if I prefix the urls with http:// the songs play fine.
Example...
If I copy the songs to my SD card both play fine and as long as the internet url strings have an 'http://' prefix then they also work.