Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing audio files one after another

I have a list of audio files like this,

int music_numbers[] = { R.raw.one, R.raw.two, R.raw.three, R.raw.four,
            R.raw.five, R.raw.six, R.raw.seven, R.raw.eight, R.raw.nine };

I need to play this one by one - when i send 100 to this play_numbers method - i am intend to play digit wise..

I have done this - but i unable to do this one after another..

private void _play_numbers(final String i) {
    // TODO Auto-generated method stub

    mPlayer = MediaPlayer.create(PlayFileActivity.this, R.raw.payment);
    mPlayer.start();

    mPlayer.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mPlayer.stop();
            myDigit = i.charAt(1) + "";

            _function_play_file(Integer.parseInt(myDigit));

        }

    });

}

in _function_play_file - i have this

void _function_play_file(int files) {

        switch (files) {
        case 0:
            mPlayer = MediaPlayer.create(this, music_numbers[0]);
            mPlayer.start();
        case 1:
            mPlayer = MediaPlayer.create(this, music_numbers[1]);
            mPlayer.start();
        case 2:
            mPlayer = MediaPlayer.create(this, music_numbers[2]);
            mPlayer.start();
        case 3:
            mPlayer = MediaPlayer.create(this, music_numbers[3]);
            mPlayer.start();
        case 4:
            mPlayer = MediaPlayer.create(this, music_numbers[4]);
            mPlayer.start();
        case 5:
            mPlayer = MediaPlayer.create(this, music_numbers[5]);
            mPlayer.start();
        case 6:
            mPlayer = MediaPlayer.create(this, music_numbers[6]);
            mPlayer.start();
        case 7:
            mPlayer = MediaPlayer.create(this, music_numbers[7]);
            mPlayer.start();
        case 8:
            mPlayer = MediaPlayer.create(this, music_numbers[8]);
            mPlayer.start();
        case 9:
            mPlayer = MediaPlayer.create(this, music_numbers[9]);
            mPlayer.start();
        case 10:
            mPlayer = MediaPlayer.create(this, music_numbers[10]);
            mPlayer.start();

        }
    }
like image 696
Sanjay Herle Avatar asked May 28 '12 08:05

Sanjay Herle


1 Answers

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.

like image 159
IncrediApp Avatar answered Oct 23 '22 18:10

IncrediApp