tags:

views:

105

answers:

1

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.

+1  A: 

A SoundPool (Docs) might be something to consider.

QRohlf
It might indeed. I've found, though, in some cases, you might find SoundPool a bit lacking in features, as it is pretty aimed at very small clips, with simple lifecycles.
torkildr