简体   繁体   中英

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=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"

I experienced similar problem with MediaPlayer

the create() method sometimes return null

Some of the file types I have tried are mp3, wav, and m4a

I know they aren't corrupt because I can play them in 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. It made me conclude that MediaPlayer can't play file above certain bitrates. big size = better quality.

So my solution is: Convert everything into .mp3 with 128kbps bitrate

EDIT: It looks like the problem is with my emulator, when I used a real Android device, everything works perfectly. Make sure that you test on real Android Device. Or in my case, I created another AVD profile with better spec and it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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