Playing audio files one after another

You need to set an onCompletionListener to each and start the next one on completion.

mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
{
    @Override
    public void onCompletion(MediaPlayer mp) 
    {
         // Code to start the next audio in the sequence
    }
});

The best way to achieve this is to create a class that implements OnCompletionListener which handles the onCompletion and receives the next file to play. This way you can instantiate it nicely in your code. Of course, don't forget your break; in the cases above.


Use a queue for holding the numbers to be played.

private void _play_numbers(final String i) {
    // e.g '100': put '1', '0', '0' in a Queue after converting to digits
    Queue queue = new LinkedList();
    //Use the add method to add items.

    myDigit = // remove next digit from queue..

    _function_play_file(myDigit);
}

void _function_play_file(int files) {
     switch(files) {
       case 0:
        mPlayer = MediaPlayer.create(PlayFileActivity.this, R.raw.payment);
        mPlayer.setOnCompletionListener(completeListener );
        mPlayer.start();
        break;
 .....
 }

OnCompletionListener completeListener = new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.release();
        myDigit = // remove next digit from queue..
        if (myDigit != -1)  // if queue is not empty..
            _function_play_file(myDigit);
    }
});

}