提问人:Kfir Arnesty 提问时间:11/9/2023 更新时间:11/9/2023 访问量:38
Soundpool 始终为 null,即使在启动它之后也是如此
soundpool always null even after initilazing it
问:
我正在尝试在我的应用程序中创建一个声音选择器,但每次我尝试使用声音池时,它都是空的。即使我初始化它,它的 stil null。 错误发生在 line: soundPool.play(correctSound,1,1,0,0,1);
这是代码的一部分:
package com.example.kfir_compare;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Options extends AppCompatActivity {
private SoundPool soundPool;
private int correctSound;
private int incorrectSound;
private int correctSoundV;
private int incorrectSoundV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
configureBackButton();
initElements();
System.out.println("here");
//Options options = new Options(this);
}
private void configureBackButton() {
Button backButton = (Button) findViewById(R.id.BB);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
}
);
}
public Options() {
// Default constructor with no arguments
}
// public Options(Context context) {
// 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(1, AudioManager.STREAM_MUSIC, 0);
// }
//
// correctSound = soundPool.load(context, R.raw.correctsoundnovoice, 1);
// incorrectSound = soundPool.load(context, R.raw.wrongsound, 1);
// correctSoundV = this.soundPool.load(context, R.raw.youarecorrectadiofile, 1);
// incorrectSoundV = this.soundPool.load(context, R.raw.youarewrongadiofile, 1);
// }
public void initializeSoundPool(Context context) {
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(1, AudioManager.STREAM_MUSIC, 0);
}
correctSound = soundPool.load(context, R.raw.correctsoundnovoice, 1);
incorrectSound = soundPool.load(context, R.raw.wrongsound, 1);
correctSoundV = this.soundPool.load(context, R.raw.youarecorrectadiofile, 1);
incorrectSoundV = this.soundPool.load(context, R.raw.youarewrongadiofile, 1);
}
private void initElements() {
}
public void playSound(View view) {
if (view.getId() == R.id.VS){
soundPool.play(correctSound,1,1,0,0,1);
soundPool.play(incorrectSound,1,1,0,0,1);
}
else {
soundPool.play(correctSoundV,1,1,0,0,1);
soundPool.play(incorrectSoundV,1,1,0,0,1);
}
}
public SoundPool getSoundPool() {
return soundPool;
}
public void setSoundPool(SoundPool soundPool) {
this.soundPool = soundPool;
}
public int getCorrectSound() {
return correctSound;
}
public void setCorrectSound(int correctSound) {
this.correctSound = correctSound;
}
public int getIncorrectSound() {
return incorrectSound;
}
public void setIncorrectSound(int incorrectSound) {
this.incorrectSound = incorrectSound;
}
}
我尝试使用 Chat GPT,但他只是让我兜圈子。请帮忙。请记住,我不熟悉声音池,这是我第一次使用它。
答: 暂无答案
评论