MediaPlayer 在模拟器上工作,但在设备上不工作
我正在开发一个 Android 广播应用程序,我发现了一个奇怪的问题。这是代码的一部分:
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(1, 1);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.setOnPreparedListener(
mediaPlayer.prepareAsync(); // prepare async to not block main thread
该程序在模拟器(2.2、2.3)上运行良好,但是当我在我的两个设备(均为 Android 2.2)上尝试时,没有音频。我已经尝试了很多解决方法,但音频在模拟器上工作,但在设备上不起作用,这似乎很奇怪。
PS:我已经设置了互联网权限
所有监听器都已启动(OnBufferingUpdateListener、OnCompletionListener、OnErrorListener、OnInfoListener、OnPreparedListener)在模拟器上我听音乐,但它在 logcat 中给出错误: 模拟器日志
这是显示的错误logcat(在设备上):
E/OMXPlayer( 953): Creating new NVOMXPlayer: 0x9c1a8
E/OMXPlayer( 953): onFirstRef ++
E/OMXPlayer( 953): onFirstRef --
E/OMXPlayer( 953): setdatasource ++
E/OMXPlayer( 953): 0x9c1a8 setDataSource url=http://XXXXXX:8000
E/OMXPlayer( 953): render thread(10972) started: 0x9c1a8
E/OMXPlayer( 953): STATE_PREPARING - signal wait 0x9c1a8
E/OMXPlayer( 953): prepareAsync: 0x9c1a8
E/OMXPlayer( 953): STATE_PREPARING - signal rx'd 0x9c1a8
E/OMXPlayer( 953): prepare 0x9c1a8
E/OMXPlayer( 953): 0x9c1a8 Creating player for: http://XXXXX:8000
D/ ( 961): NVRM_DAEMON(803): rt_exist=2, add client ref
D/ ( 953): NvMMSetUserAgentString:: Len: 49: String: User-Agent: (NvMM Client v0.1; Linux;Android 2.2)
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): Event_BlockError from 13SuperParser : Error code - 4
D/ ( 953): Graph_EventHandler: ERROR for READER [0x80001005]
D/ ( 953): Graph_EventHandler: ERROR for READER [0x80001005]
E/MediaPlayer(10958): error (1, 0)
E/ (10958): Prepare failed.: status=0x1
E/ (10958): java.io.IOException: Prepare failed.: status=0x1
E/ (10958): at android.media.MediaPlayer.prepare(Native Method)
E/ (10958): at app.relax.com.StreamingMediaPlayer.listen(StreamingMediaPlayer.java:64)
E/ (10958): at app.relax.com.StreamingMediaPlayer.startListening(StreamingMediaPlayer.java:74)
E/ (10958): at app.relax.com.StreamingMediaPlayer.access$0(StreamingMediaPlayer.java:70)
E/ (10958): at app.relax.com.StreamingMediaPlayer$1.run(StreamingMediaPlayer.java:51)
E/ (10958): at java.lang.Thread.run(Thread.java:1096)
E/OMXPlayer( 953): prepare 0x9c1a8 exit with error
E/OMXPlayer( 953): Error in prepare
E/OMXPlayer( 953): Worker Thread Exit -
I am working on a radio app for Android and I've found a strange issue. That's a part of the code:
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(1, 1);
try {
mediaPlayer.setDataSource(url);
mediaPlayer.setOnPreparedListener(
mediaPlayer.prepareAsync(); // prepare async to not block main thread
The program works well on the emulator (2.2, 2.3), but when I try on my two devices (both Android 2.2) there is no audio. I've tried a lot of workarounds, but it seems so strange that Audio works on emulator, but not on devices.
PS: I've set Internet permissions
All the listiners are up (OnBufferingUpdateListener, OnCompletionListener, OnErrorListener, OnInfoListener, OnPreparedListener) On emulator I listen the music, but it gives error in logcat: emulator log
This is the error showed in logcat (on device):
E/OMXPlayer( 953): Creating new NVOMXPlayer: 0x9c1a8
E/OMXPlayer( 953): onFirstRef ++
E/OMXPlayer( 953): onFirstRef --
E/OMXPlayer( 953): setdatasource ++
E/OMXPlayer( 953): 0x9c1a8 setDataSource url=http://XXXXXX:8000
E/OMXPlayer( 953): render thread(10972) started: 0x9c1a8
E/OMXPlayer( 953): STATE_PREPARING - signal wait 0x9c1a8
E/OMXPlayer( 953): prepareAsync: 0x9c1a8
E/OMXPlayer( 953): STATE_PREPARING - signal rx'd 0x9c1a8
E/OMXPlayer( 953): prepare 0x9c1a8
E/OMXPlayer( 953): 0x9c1a8 Creating player for: http://XXXXX:8000
D/ ( 961): NVRM_DAEMON(803): rt_exist=2, add client ref
D/ ( 953): NvMMSetUserAgentString:: Len: 49: String: User-Agent: (NvMM Client v0.1; Linux;Android 2.2)
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): connect cancelled
D/ ( 953): Event_BlockError from 13SuperParser : Error code - 4
D/ ( 953): Graph_EventHandler: ERROR for READER [0x80001005]
D/ ( 953): Graph_EventHandler: ERROR for READER [0x80001005]
E/MediaPlayer(10958): error (1, 0)
E/ (10958): Prepare failed.: status=0x1
E/ (10958): java.io.IOException: Prepare failed.: status=0x1
E/ (10958): at android.media.MediaPlayer.prepare(Native Method)
E/ (10958): at app.relax.com.StreamingMediaPlayer.listen(StreamingMediaPlayer.java:64)
E/ (10958): at app.relax.com.StreamingMediaPlayer.startListening(StreamingMediaPlayer.java:74)
E/ (10958): at app.relax.com.StreamingMediaPlayer.access$0(StreamingMediaPlayer.java:70)
E/ (10958): at app.relax.com.StreamingMediaPlayer$1.run(StreamingMediaPlayer.java:51)
E/ (10958): at java.lang.Thread.run(Thread.java:1096)
E/OMXPlayer( 953): prepare 0x9c1a8 exit with error
E/OMXPlayer( 953): Error in prepare
E/OMXPlayer( 953): Worker Thread Exit -
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
流的媒体类型是什么?也许您在生产设备上缺少该格式的编解码器。
老实说,生产设备往往比模拟器具有更好的编解码器,因为 2.x 设备通常具有 WMA 以及 MP3 和 MP4。但也许这会破坏一些东西。
如果它是播放列表格式(例如 m3u)或复合格式(atom?rss?或信封中具有多种格式的多个流 URL 的其他格式),则制作设备可能会选择一种流式传输效果不太好的格式。例如,他们选择了 WMA 流,但只有当他们开始解码时才发现它具有 DRM。
网络广播确实经常使用某种复合格式。他们经常在 WMA 流中添加 DRM。实际上,那将是一个很好的候选人。
无论如何,2.x 流媒体支持相当薄弱。如果您能打破信封并选择 MP3 或 MP4 而不是 WMA,您可能会做得更好。
但第一个问题是:流的格式是什么。
我认为也有可能:如果您的生产设备已获得 root 权限,则会禁用受 DRM 保护的文件的解码。用未root的设备尝试一下?
最后一种可能性:2.x 设备使用各种音频堆栈。 NVOMXPlayer 看起来并不熟悉。它们是 Atrix 设备吗?
What's the media type of the stream? Perhaps you're missing CODECs for that format on the production devices.
Honestly, the production devices tend have better CODECs than the emulators because the 2.x devices often have WMA as well as MP3 and MP4. But maybe that breaks something.
If it's playlist format (e.g. m3u), or a compound format (atom? rss?, or some other format with multiple stream URLs in multiple formats in an envelope), maybe the production devices pick a format that doesn't stream so well. e.g. they pick a WMA stream, but find out that it has DRM only when they start decoding.
Internet radio does often uses some kind of compound format. And they often put DRM on their WMA streams. That would be a good candidate, actually.
For what it's worth, 2.x streaming support is pretty weak. If you can crack the envelope and pick MP3 or MP4 instead of WMA you may do much better.
But the first question is: what's the format of the stream.
Also vaguely possible, I suppose: if your production devices are rooted, that disables decoding of DRM-protected files. Try it with an unrooted device?
Last possibility: 2.x devices use a variety of audio stacks. NVOMXPlayer doesn't look familiar. Are they Atrix devices?