Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

android - 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();

        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...