循环完成后播放声音

时间:2018-04-23 08:54:44

标签: android loops

我想在使用此架构点击按钮时播放原始文件夹中的声音

首先打开声音,然后是来自edittext的信件的声音,以及最后的关闭声音,

但我只是播放第一个文件,

这是我的onClick代码

int[] openSound ={R.raw.ding,R.raw.open};
int[] closeSound ={R.raw.close,R.raw.dong};
int[] myMusic;
String editTextValue = yourEditText.getText().toString().toLowerCase();
final int length = editTextValue.length();
int[] myMusic = new int[length];
for (int i = 0; i < length; i++) {
char letter = editTextValue.charAt(i);
myMusic[i] = getResources().getIdentifier(String.valueOf(letter), "raw", getPackageName());}
PlayMedia playOpen = new PlayMedia(MainActicity,openSound);
playOpen.execute();

PlayMedia playAudio = new PlayMedia(MainActivity.this,myMusic);
playAudio.execute();

PlayMedia playClose = new PlayMedia(MainActivity.this,closeSound);
playClose.execute();

2 个答案:

答案 0 :(得分:0)

我发现我的问题的答案是加入所有的int [],然后用PlayMedia播放

int[] concatenate = combine(array1, array2);
    PlayMedia playAudio = new PlayMedia(MainActivity.this, concatenate);
    playAudio.execute();

}


public static int[] combine(int[] a, int[] b, int[] c){
    int length = openSound.length + myMusic.length+closeSound.length;
    int[] result = new int[length];
    System.arraycopy(openSound, 0, result, 0, openSound.length);
    System.arraycopy(myMusic, 0, result, openSound.length, myMusic.length);
    System.arraycopy(closeSound, 0, result, openSound.length+myMusic.length, closeSound)
    return result;
}

谢谢

答案 1 :(得分:-1)

除非您为完成事件添加回调,否则它无法正常工作。

您可以看到文档here和示例:

  yourPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() 
    {
        @Override
        public void onCompletion(MediaPlayer mp) 
        {
             // Here add the execute of the next audio, +
             // and another callback if you want to concatenate more.
        }
    });