使用 MediaPlayer 从 Android 中的 URL 流式传输音频?
我一直在尝试使用 Android 内置的 MediaPlayer 类通过 http 流式传输 mp3。该文档向我建议,这应该像以下一样简单:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(URL_OF_FILE);
mp.prepare();
mp.start();
但是我反复收到以下信息。我也尝试过不同的网址。请不要告诉我流媒体不适用于 mp3。
E/PlayerDriver( 31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
W/PlayerDriver( 31): PVMFInfoErrorHandlingComplete
E/MediaPlayer( 198): error (1, -4)
E/MediaPlayer( 198): start called in state 0
E/MediaPlayer( 198): error (-38, 0)
E/MediaPlayer( 198): Error (1,-4)
E/MediaPlayer( 198): Error (-38,0)
非常感谢任何帮助,谢谢 S
I've been trying to stream mp3's over http using Android's built in MediaPlayer class. The documentation would suggest to me that this should be as easy as :
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(URL_OF_FILE);
mp.prepare();
mp.start();
However I am getting the following repeatedly. I have tried different URLs as well. Please don't tell me that streaming doesn't work on mp3's.
E/PlayerDriver( 31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
W/PlayerDriver( 31): PVMFInfoErrorHandlingComplete
E/MediaPlayer( 198): error (1, -4)
E/MediaPlayer( 198): start called in state 0
E/MediaPlayer( 198): error (-38, 0)
E/MediaPlayer( 198): Error (1,-4)
E/MediaPlayer( 198): Error (-38,0)
Any help much appreciated, thanks
S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
带有流媒体的简单媒体播放器示例。对于 xml 部分,您需要一个 ID 为“button1”的按钮,以及可绘制文件夹中名为“button_pause”和“button_play”的两个图像,请不要忘记在清单中添加互联网权限。
simple Media Player with streaming example.For xml part you need one button with id button1 and two images in your drawable folder with name button_pause and button_play and please don't forget to add the internet permission in your manifest.
Android MediaPlayer 在 2.2 之前不支持本地 MP3 流。在旧版本的操作系统中,它似乎仅本地传输 3GP。您可以尝试 pocketjourney 代码,尽管它很旧(有一个 这里是新版本),我很难让它粘起来——每当它重新填充缓冲区时它就会结结巴巴。
适用于 Android 的 NPR 新闻应用程序是开源的,在 2.2 之前的操作系统版本中使用本地代理服务器来处理 MP3 流。您可以在此处查看第 199-216 行(r94)中的相关代码:http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android /news/PlaybackService.java?r=7cf2352b5c3c0fbcdc18a5a8c67d836577e7e8e3
这是 StreamProxy 类:http://code.google.com/p/npr-android-app/source/browse/Npr/src/ org/npr/android/news/StreamProxy.java?r=e4984187f45c39a54ea6c88f71197762dbe10e72
NPR 应用程序有时在流式传输时仍然会收到“错误 (-38, 0)”。这可能是线程问题或网络更改问题。检查问题跟踪器以获取更新。
Android MediaPlayer doesn't support streaming of MP3 natively until 2.2. In older versions of the OS it appears to only stream 3GP natively. You can try the pocketjourney code, although it's old (there's a new version here) and I had trouble making it sticky — it would stutter whenever it refilled the buffer.
The NPR News app for Android is open source and uses a local proxy server to handle MP3 streaming in versions of the OS before 2.2. You can see the relevant code in lines 199-216 (r94) here: http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/PlaybackService.java?r=7cf2352b5c3c0fbcdc18a5a8c67d836577e7e8e3
And this is the StreamProxy class: http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java?r=e4984187f45c39a54ea6c88f71197762dbe10e72
The NPR app is also still getting the "error (-38, 0)" sometimes while streaming. This may be a threading issue or a network change issue. Check the issue tracker for updates.
我猜您正在尝试直接播放 .pls 或类似的内容。
试试这个:
1:代码
2:.pls 文件
此 URL 来自 BBC,仅供参考。这是一个 .pls 文件,我在 Linux 上下载
,然后用 vim 打开(使用你最喜欢的编辑器;),我看到了这个文件中的真实 URL。不幸的是,并非所有 .pls 都是这样的纯文本。
我读到 1.6 不支持通过 http 传输 mp3,但是,我刚刚使用 android 1.6 和 2.2 测试了上述代码,没有任何问题。
祝你好运!
I guess that you are trying to play an .pls directly or something similar.
try this out:
1: the code
2: the .pls file
This URL is from BBC just as an example. It was an .pls file that on linux i downloaded with
and then i opened with vim (use your favorite editor ;) and i've seen the real URLs inside this file. Unfortunately not all of the .pls are plain text like that.
I've read that 1.6 would not support streaming mp3 over http, but, i've just tested the obove code with android 1.6 and 2.2 and didn't have any issue.
good luck!
使用
Use
我遇到了和你一样的错误,结果证明代码没有问题。问题是网络服务器发送了错误的 Content-Type 标头。
尝试使用wireshark或类似的工具来查看网络服务器发送的内容类型。
I've had the same error as you have and it turned out that there was nothing wrong with the code. The problem was that the webserver was sending the wrong Content-Type header.
Try wireshark or something similar to see what content-type the webserver is sending.
查看我的项目:
Looking my projects:
不使用 OnPreparedListener 调用 mp.start 以避免日志中的零状态。
No call mp.start with an OnPreparedListener to avoid the zero state i the log..