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