我如何从声音中播放数组? - Android

时间:2014-12-12 15:02:30

标签: java android arrays android-mediaplayer

我创建一个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();}
}

2 个答案:

答案 0 :(得分:1)

在播放下一个声音之前,您应该等待每个声音完成播放:

mediaplayer.setOnCompletionListener将允许您创建一个可以读取下一个声音的侦听器。 (设置计数器字段以了解要发挥的女巫声音。

希望它有所帮助!

答案 1 :(得分:0)

见:

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