Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaPlayer.create() always returns null

I have used the mediaplayer before, and I have never had this problem. Whenever I try to use MediaPlayer.create(), the method gives me null, and I can't play my sounds. Is there anything that I am missing?

public class Game extends Activity
{
    private MediaPlayer mp = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

        mp = MediaPlayer.create(getBaseContext(), R.raw.sound);
    }
}

my sound.mp3 is in my raw folder, which I placed in there by draging my sound to the folder in eclipse. Please help, since I have played sound before, this is really bugging me :(

like image 857
Keerigan Avatar asked Feb 22 '23 14:02

Keerigan


2 Answers

The create() API returns null if it somehow failed.

The reason in this case is due to a corrupt mp3 file.

like image 140
Reno Avatar answered Mar 03 '23 12:03

Reno


In my case, it was the emulator (AVD)

Make sure that you also test it using a real Android device

or, create a new AVD profile with better spec and newer Android version

What seems to always work regardless of which AVD is using 128kbps mp3 audio file

like image 32
grandia Avatar answered Mar 03 '23 11:03

grandia