使用 Android MediaPlayer 进行音频流传输
我正在尝试使用 Android 的 MediaPlayer 创建音频流媒体。如果它不适用于 Android 2.1 或更低版本也没关系。我需要能够播放 SHOUTcast 流中的音频。这是我的代码:
player = new MediaPlayer();
try {
player.setDataSource("http://87.230.103.107:8000");
player.prepareAsync();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
由于某种原因,该代码什么也不会播放。我认为这可能与应用程序权限有关。有谁知道发生了什么事吗?
谢谢!
[更新] 我收到以下错误:
04-25 23:35:15.432: ERROR/MediaPlayer(283): start called in state 4
04-25 23:35:15.432: ERROR/MediaPlayer(283): error (-38, 0)
04-25 23:35:15.602: ERROR/MediaPlayer(283): Error (-38,0)
04-25 23:35:17.542: INFO/AwesomePlayer(33): calling prefetcher->prepare()
04-25 23:35:18.547: INFO/Prefetcher(33): [0x17650] cache below low water mark, filling cache.
04-25 23:35:18.762: INFO/AwesomePlayer(33): prefetcher is done preparing
04-25 23:35:19.769: ERROR/AwesomePlayer(33): Not sending buffering status because duration is unknown.
I'm trying to create an audio streamer with Android's MediaPlayer. It's just fine if it doesn't work with Android 2.1 or bellow. I need to be able to play the audio from a SHOUTcast stream. Here's my code:
player = new MediaPlayer();
try {
player.setDataSource("http://87.230.103.107:8000");
player.prepareAsync();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
For some reason, this code will play just nothing. I think it may be related to the app permissions. Does anyone know what's going on?
Thanks!
[UPDATE]
I'm getting the following errors:
04-25 23:35:15.432: ERROR/MediaPlayer(283): start called in state 4
04-25 23:35:15.432: ERROR/MediaPlayer(283): error (-38, 0)
04-25 23:35:15.602: ERROR/MediaPlayer(283): Error (-38,0)
04-25 23:35:17.542: INFO/AwesomePlayer(33): calling prefetcher->prepare()
04-25 23:35:18.547: INFO/Prefetcher(33): [0x17650] cache below low water mark, filling cache.
04-25 23:35:18.762: INFO/AwesomePlayer(33): prefetcher is done preparing
04-25 23:35:19.769: ERROR/AwesomePlayer(33): Not sending buffering status because duration is unknown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉回复晚了,在寻找另一个问题的答案时遇到了这个问题。
如果您仍需要答案,请在准备就绪时调用
start()
。与prepare()
不同,PrepareAsync()
会立即返回。对流使用“prepare()”的问题是,它会阻塞,直到有足够的数据来开始播放。您想要做的是设置一个
OnPreparedListener
,并从那里调用start()
。Sorry for the late response, ran across this while looking for answers to another problem.
If you still need an answer, you're calling
start()
while it's still being prepared.PrepareAsync()
returns immediately, unlikeprepare()
. The problem with using 'prepare()` with streams is that it will block until it has enough data to start play.What you want to do is set an
OnPreparedListener
, and have thestart()
called from there.问题是不直接支持内容类型“audio/aacp”流。某些解码库可以播放“aacp”,请参阅以下解决方案:
免费软件高级音频(AAC) Android 解码器
如何使用这个库?
有关更多详细信息,请参阅这个一起工作。
使用时考虑法律问题.
The problem is that content type "audio/aacp" streaming is not supported directly . Some decoding library can be sued to play "aacp", please see the solution below:
Freeware Advanced Audio (AAC) Decoder for Android
How to use this library?
For more detail please see this.
Consider legal issues while using it.
1)避免使用prepare(),而是使用prepareAsyc()。 (或)将您的播放逻辑放入工作线程或单独的线程中。 1)避免使用player.prepare(),而是使用player.prepareAsync();
(或)将逻辑保留在单独的线程中。 (您可以使用 Intent Service、AsyncTask 等)
2)还可以尝试使用静态构造函数 MediaPlayer.create(Uri);
1)Avoid using prepare(), use prepareAsyc() instead. (or) put your playing logic in a worked thread or a separate thread. 1)Avoid using player.prepare(), instead use player.prepareAsync();
(or) keep the logic in a separate thread. (you can use intent service, AsyncTask,etc)
2)Also try with static constructor MediaPlayer.create(Uri);