私のプログラムでsoundpoolクラスを使用することに問題があります。私はサウンドプールビルダを開始し、問題なくサウンドをロードしました。特定のサウンドをオン/オフする複数のトグルボタンを使用できるようにしたい。トグルボタンのオン/オフを切り替えることができますが、たとえば、2つ以上のトグルボタンをオンにして1つを一時停止すると、残りの部分が停止します。私はそれを望んでいない、私は他の音が奏で続けることを望む。ここに私のコードです。また、誰かが私がそれを助けることができるビットをきれいにすることができます。前もって感謝します。Android、Soundpoolクラス - ループ中に自動終了の問題が発生する
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;
長さが何でありますかサウンドファイルの? –
短いサンプルで、おそらく5〜10秒です。 – wth7777