gpt4 book ai didi

java - Android,Soundpool 类 - 循环播放声音时出现自动暂停问题

转载 作者:行者123 更新时间:2023-11-30 10:45:48 25 4
gpt4 key购买 nike

我在使用 soundpool 类的程序中遇到问题。我启动了 soundpool builder 并毫无问题地加载了我的声音。我希望能够使用多个切换按钮来打开和关闭特定的声音。我可以打开和关闭切换按钮,但是例如,如果我打开两个或多个切换按钮,然后暂停一个……其余的将停止。我不想这样,我希望其他声音继续播放。这是我的代码。另外,如果有人可以帮我清理一下,那会很有帮助。提前致谢。

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bT1 = (ToggleButton) findViewById(R.id.firstToggle);
bT2 = (ToggleButton) findViewById(R.id.secondToggle);
bT1.setOnClickListener(this);
bT2.setOnClickListener(this);
bT3 = (ToggleButton) findViewById(R.id.thirdToggle);
bT4 = (ToggleButton) findViewById(R.id.fourthToggle);
bT3.setOnClickListener(this);
bT4.setOnClickListener(this);
bT5 = (ToggleButton) findViewById(R.id.fifthToggle);
bT6 = (ToggleButton) findViewById(R.id.sixToggle);
bT5.setOnClickListener(this);
bT6.setOnClickListener(this);
bT7 = (ToggleButton) findViewById(R.id.seventhToggle);
bT8 = (ToggleButton) findViewById(R.id.eighthToggle);
bT7.setOnClickListener(this);
bT8.setOnClickListener(this);

initionalizeSoundpool();
}

private void initionalizeSoundpool() {

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

ourSounds = new SoundPool.Builder()
.setMaxStreams(8)
.setAudioAttributes(audioAttributes)
.build();
soundFx = ourSounds.load(this, R.raw.highhatandrims, 1);
beatOne = ourSounds.load(this, R.raw.technodrums, 1);
soundFx2 = ourSounds.load(this, R.raw.soothing, 1);
beatTwo = ourSounds.load(this, R.raw.thumpohyeahbeat, 1);
soundFx3 = ourSounds.load(this, R.raw.dreamyone, 1);
beatThree = ourSounds.load(this, R.raw.atredundantsynthesis, 1);
soundFx4 = ourSounds.load(this, R.raw.tawaka, 1);
clap = ourSounds.load(this, R.raw.cabessa, 1);
} else {
ourSounds = new SoundPool(4, AudioManager.STREAM_MUSIC,1);
soundFx = ourSounds.load(this, R.raw.highhatandrims, 1);
beatOne = ourSounds.load(this, R.raw.technodrums, 1);
soundFx2 = ourSounds.load(this, R.raw.soothing, 1);
beatTwo = ourSounds.load(this, R.raw.thumpohyeahbeat, 1);
soundFx3 = ourSounds.load(this, R.raw.dreamyone, 1);
beatThree = ourSounds.load(this, R.raw.atredundantsynthesis, 1);
soundFx4 = ourSounds.load(this, R.raw.tawaka, 1);
clap = ourSounds.load(this, R.raw.cabessa, 1);
}

}




public void onClick(View view) {
switch (view.getId()) {
case R.id.firstToggle:
if(bT1.isChecked()) {
ourSounds.play(soundFx, 1, 1, 1, -1, 1);
}
else {
ourSounds.autoPause();
}
break;
case R.id.secondToggle:
if(bT2.isChecked()) {
ourSounds.play(beatOne, 1, 1, 1, -1, 1);
}
else {
ourSounds.autoPause();
}
break;
case R.id.thirdToggle:
if(bT3.isChecked()) {
ourSounds.play(soundFx2, 1, 1, 1, -1, 1);
}
else {
ourSounds.autoPause();
}
break;
case R.id.fourthToggle:
if(bT4.isChecked()) {
ourSounds.play(beatTwo, 1, 1, 1, -1, 1);
}
else {
ourSounds.autoPause();
}
break;

最佳答案

将此添加到您的 OnPause 方法:

@Override
public void onPause() {
super.onPause();

ourSounds.release();
ourSounds = null;
}

更新:

请尝试使用 ourSounds.autoPause(); 而不是 ourSounds.autoPause(); 例如:

case R.id.firstToggle:
if(bT1.isChecked()) {
ourSounds.play(soundFx, 1, 1, 1, -1, 1);
}
else {
ourSounds.pause(soundFx);
}
break;
...

关于java - Android,Soundpool 类 - 循环播放声音时出现自动暂停问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com