简体   繁体   English

android mediaplayer.create返回null

[英]android mediaplayer.create returns null

I'm trying to play sound from URI Using MediaPlayer Class but when i tried this my app crashed then i noticed that mediaplayer.create() function returns null what can i do to solve this problem?` 我正在尝试使用MediaPlayer Class从URI播放声音,但是当我尝试此操作使我的应用程序崩溃时,我注意到mediaplayer.create()函数返回null,我该怎么办才能解决此问题?

mediaplayer=MediaPlayer.create(this,Uri.parse("http://example.com/files/music.mp3"));
if (mediaplayer==null){
    Toast.makeText(this,"media player is null",Toast.LENGTH_LONG).show();
        }
else
    mediaplayer.start();

now when i run my app it shows Toast message "media player is null" 现在,当我运行我的应用程序时,它显示Toast消息“媒体播放器为空”

I experienced similar problem with MediaPlayer 我在MediaPlayer中遇到了类似的问题

the create() method sometimes return null create()方法有时会返回null

Some of the file types I have tried are mp3, wav, and m4a 我尝试过的某些文件类型是mp3,wav和m4a

I know they aren't corrupt because I can play them in winamp/itunes 我知道它们没有损坏,因为我可以在winamp / itunes中播放它们

While the documentation does say that it supports those file type, it turns out that it actually doesn't 虽然文档确实说它支持那些文件类型,但事实证明它实际上不支持

I noticed that when the audio file (regardless of file type) is 10Mb or more then the MediaPlayer will return null. 我注意到,当音频文件(与文件类型无关)为10Mb或更大时,MediaPlayer将返回null。 It made me conclude that MediaPlayer can't play file above certain bitrates. 这使我得出结论,MediaPlayer无法播放高于某些比特率的文件。 big size = better quality. 大尺寸=更好的质量。

So my solution is: Convert everything into .mp3 with 128kbps bitrate 所以我的解决方案是:用128kbps比特率将所有内容转换为.mp3

EDIT: It looks like the problem is with my emulator, when I used a real Android device, everything works perfectly. 编辑:看起来问题出在我的模拟器上,当我使用真正的Android设备时,一切运行正常。 Make sure that you test on real Android Device. 确保您在真实的Android设备上进行测试。 Or in my case, I created another AVD profile with better spec and it works 或者以我为例,我创建了另一个具有更好规格的AVD配置文件,并且可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM