使用SoundPool顺序播放两个声音

时间:2019-03-07 12:49:28

标签: android r.java-file soundpool

实际上,我想按顺序播放原始文件夹中的所有声音。当我单击按钮时,只会播放第一个按钮。感谢您的帮助。这是我的代码:

public class MainActivity extends AppCompatActivity {
private SoundPool soundPool;
private int sound1, sound2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();

        soundPool = new SoundPool.Builder()
                .setMaxStreams(2)
                .setAudioAttributes(audioAttributes)
                .build();
    } else {
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
    }

    sound1 = soundPool.load(this, R.raw.a, 1);
    sound2 = soundPool.load(this, R.raw.d, 1);

}
public void playSound(View v) {
    soundPool.play(sound1, 1, 1, 0, 0, 1);
    soundPool.play(sound2, 1, 1, 0, 0, 1);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    soundPool.release();
    soundPool = null;
}
}

0 个答案:

没有答案