我创建了Array
声音,然后需要播放它们,但是我只能听到第一个声音:
这是我的Array
:
public static class Marrays{
public static String[] Zero(){
String[] OK = new String[] {"num1.mp3" , "increment.amr", "num1.mp3"};
return OK;
}
}
和 :
String[] a = Marrays.Zero();
我从上面使用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();}
}
看到这个:
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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句