Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SDK Mediaplayer.create randomly returns null

Tags:

android

media

I am having an issue where occasionally the MediaPlayer.create method will return null, even though the audio file is definitely there. In fact, if I put the call to create into a while loop, the media player will eventually be created successfully. This only seems to happen on my phone (HTC Hero running 2.1) and never in the emulator.

Is this just an issue with the phone? Or is there a different way I should be playing these audio files?

Note that I want to be loading them dynamically which is get I am making the call to getIndentifer. If there is a better way of playing these dynamically please let me know.

public void playAudio(String audioFile){

    //instance variable of type MediaPlayer
    _player = null;
    while(_player == null){
         _player = MediaPlayer.create(_context, getResources().getIdentifier(audioFile, "raw", _context.getPackageName()));
    }
    _player.start();

}


Thanks.

like image 397
Mr Zorn Avatar asked Aug 28 '10 03:08

Mr Zorn


1 Answers

A SoundPool (Docs) might be something to consider.

like image 158
QRohlf Avatar answered Nov 18 '22 04:11

QRohlf