Reputation:
I create an Array
of sound and then I need to play them but I can hear just first sound :
This my Array
:
public static class Marrays{
public static String[] Zero(){
String[] OK = new String[] {"num1.mp3" , "increment.amr", "num1.mp3"};
return OK;
}
}
And :
String[] a = Marrays.Zero();
And I use from above array
:
for (int i = 0; i < a.length; i++) {
try {
AssetFileDescriptor afd = getAssets().openFd(a[i]);
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mediaPlayer.prepare();
mediaPlayer.start();
}
catch (IllegalArgumentException e) { Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show(); }
catch (IllegalStateException e) {Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show(); }
catch (IOException e) { Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show();}
}
Upvotes: 1
Views: 1049
Reputation: 1148
See this :
if(count < 3){
Main_Sound_mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if(count <= 3){
try {
AssetFileDescriptor afd = getAssets().openFd(a[count]);
if (Main_Sound_mediaPlayer.isPlaying()==true ){
Main_Sound_mediaPlayer.stop();}
Main_Sound_mediaPlayer.reset();
Main_Sound_mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd .getLength());
Main_Sound_mediaPlayer.prepare();
Main_Sound_mediaPlayer.start();
}
catch (IllegalArgumentException e) { Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show(); }
catch (IllegalStateException e) {Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show(); }
catch (IOException e) { Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show();}
}
count++ ;
}
});
if (flag == true) {
try {
AssetFileDescriptor afd = getAssets().openFd(a[count]);
if (Main_Sound_mediaPlayer.isPlaying()==true ){
Main_Sound_mediaPlayer.stop();}
Main_Sound_mediaPlayer.reset();
Main_Sound_mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
Main_Sound_mediaPlayer.prepare();
Main_Sound_mediaPlayer.start();
}
catch (IllegalArgumentException e) { Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show(); }
catch (IllegalStateException e) {Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show(); }
catch (IOException e) { Toast.makeText(getApplicationContext(), e.toString() , Toast.LENGTH_SHORT).show();}
flag = false;
}
count++ ;
}else {
if (Main_Sound_mediaPlayer.isPlaying()==true ){
Main_Sound_mediaPlayer.stop();}
Main_Sound_mediaPlayer.reset();
}
Upvotes: 0
Reputation: 2182
You should probably wait for each sound to finish playing, before you play the next one :
mediaplayer.setOnCompletionListener
will allow you to create a listener where you can read the next sound. (Set a counter field to know witch sound to play.
Hope it helps!
Upvotes: 1