How to Play 3 audios consecutively in Android Studio?

1

How about friends, I have three files, if I do the following within the "onclick" of my main activity:

MediaPlayer Audio1 = MediaPlayer .create(this, R.raw.Audio1);

MediaPlayer Audio2 = MediaPlayer .create(this, R.raw.Audio2);

MediaPlayer Audio3 = MediaPlayer .create(this, R.raw.Audio3);


Audio2.start();

Audio3.start();

They are reproduced simultaneously. I would like someone to give me a code to solve this, I have been researching options like MediaListener and SoundPool but the answers I find are complicated and I do not understand how to apply them. If someone had a simple and direct code that works, I would appreciate it, then I will study it in detail but I urgently need to resolve this issue.

    
asked by Marco Ycaza Lengua 29.05.2017 в 02:44
source

2 answers

0

You only need a MediaPlayer for the reproduction of the audios, if you are going to store them in /raw you can create an array with integer elements for the reproduction.

Always remember that resources must be defined with lowercase names with letters from a-z, 0-9, or underscore "_":

   private int[] sounds = {R.raw.audio1, R.raw.audio2, R.raw.audio3};

by completing the playback of an audio detected through OnCompletionListener , continue with the next audio in turn.

This would be the complete example:

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements MediaPlayer.OnCompletionListener {

    private final static String TAG = "MediaPlayer audios";
    private MediaPlayer mediaPlayer;
    private int[] sounds = {R.raw.audio1, R.raw.audio2, R.raw.audio3};
    private int sound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = MediaPlayer.create(this, sounds[0]);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        play();
    }

    private void play() {
        sound++;
        if (sounds.length <= sound){
            //Termina reproducción de todos los audios.
            return;
        }

        AssetFileDescriptor afd = this.getResources().openRawResourceFd(sounds[sound]);

        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
            mediaPlayer.prepare();
            mediaPlayer.start();
            afd.close();
        }
        catch (IllegalArgumentException e) {
            Log.e(TAG, "IllegalArgumentException Unable to play audio : " + e.getMessage());
        }
        catch (IllegalStateException e) {
            Log.e(TAG, "IllegalStateException Unable to play audio : " + e.getMessage());
        }
        catch (IOException e) {
            Log.e(TAG, "IOException Unable to play audio : " + e.getMessage());
        }
    }
}
    
answered by 29.05.2017 / 18:57
source
1

I do not know if I understand well but you want that when you finish a song another one is played ?, you can do it with the function setOnCompletionListener

Audio1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        //Cuando acabe hara esta accion 
                        mp=MediaPlayer.create(MainActivity.this,Uri.parse(String.valueOf("/sdcard/rune.mp3")));
                        mp.start()
                    }
                });
    
answered by 29.05.2017 в 05:25