gpt4 book ai didi

java - 使用SoundPool、Timer和TimerTask每分钟播放一次随机声音

转载 作者:行者123 更新时间:2023-12-01 17:59:28 25 4
gpt4 key购买 nike

我试图每分钟播放一个随机声音,提示用户与设备交互。我正在使用 SoundPool、Timer 和 TimerTask 来尝试实现此目的。当播放声音时,我还使用 Logcat 来查看要播放的声音的 ID(这用于研究和数据分析目的)。我尝试了几种方法,包括在 TimerTask run() 方法内部和外部初始化 SoundPool,以及在单击按钮后释放 SoundPool。 timer.Schedule 用于安排 TimerTask 运行,我应该将它们设为静态吗?

logcat 正在记录 SoundId,但声音播放不一致。例如,它将播放第一个声音并记录,然后它将记录其他 SoundId,而不播放该声音。即使已按下按钮且已释放 SoundPool 的资源,也会发生这种情况。有时声音甚至根本不会播放。

任何人都可以告诉我为什么这会不一致或者更好的方法吗?

public class TouchscreenDashboard extends AppCompatActivity implements View.OnClickListener {

private SoundPool soundPool;
int[] sounds={R.raw.decrease_fan_speed_by_2, R.raw.decrease_fan_speed_by_5,
R.raw.increase_fan_speed1, R.raw.increase_fan_speed_by_3,R.raw.lower_temp1,
R.raw.lower_temp4, R.raw.raise_temp2, R.raw.raise_temp3 ,R.raw.skip_backwards_2_songs,
R.raw.skip_backwards_4_songs,R.raw.skip_forward_1_song, R.raw.skip_forward_5_songs,
R.raw.volume_down3,R.raw.volume_down6, R.raw.volume_up3, R.raw.volume_up5};

private Button volumeUp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Log.i("DashboardActivity", "Dashboard has started");

Timer timer = new Timer() {};
TimerTask timerTask = new TimerTask() {
@Override
public void run() {

AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.build();

soundPool = new SoundPool.Builder()
.setAudioAttributes(audioAttributes)
.build();

Random r = new Random();
int Low = 0;
int High = 16;
int random = r.nextInt(High-Low) + Low;
int soundId = soundPool.load(getApplicationContext(), sounds[random], 1);

soundPool.play(soundId, 1, 1, 0, 0, 1);
Log.i("DashboardActivity", "SoundId: " + random);
}
};
timer.schedule(timerTask, 60000, 60000);

volumeUp = findViewById(R.id.volumeUp);

}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.volumeUp:
if (volumeCount < 99) {
volumeCount++;
volumeText.setText(Integer.toString(volumeCount));
}
soundPool.release();
Log.i("DashboardActivity", "Volume Up Clicked");
break;
}
}
}

最佳答案

所以,经过一番努力,我有了一个可行的解决方案!我仍然认为必须有更好的方法来做到这一点,但这就是我想出的方法。 setOnLoadCompleteListener 现在仅在加载后播放声音,并且timerTask2 比timerTask1 晚5 秒运行。延迟 5 秒的原因是我的声音长度都在 1-3 秒以下。

public class TouchscreenDashboard extends AppCompatActivity implements View.OnClickListener {

int[] sounds={R.raw.decrease_fan_speed_by_2, R.raw.decrease_fan_speed_by_5,
R.raw.increase_fan_speed1, R.raw.increase_fan_speed_by_3,R.raw.lower_temp1,
R.raw.lower_temp4, R.raw.raise_temp2, R.raw.raise_temp3 ,R.raw.skip_backwards_2_songs,
R.raw.skip_backwards_4_songs,R.raw.skip_forward_1_song, R.raw.skip_forward_5_songs,
R.raw.volume_down3,R.raw.volume_down6, R.raw.volume_up3, R.raw.volume_up5};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Log.i("DashboardActivity", "Dashboard has started");

startTimer();

}

public void startTimer() {
timer = new Timer();
timer2 = new Timer();
initializeTimerTask();
initializeTimerTask2();
timer.schedule(timerTask, randomTimeInt, randomTimeInt); //
timer2.schedule(timerTask2, randomTimeInt + 5000, randomTimeInt);
}

public void initializeTimerTask() {

timerTask = new TimerTask() {
@Override
public void run() {

audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.build();
soundPool = new SoundPool.Builder()
.setAudioAttributes(audioAttributes)
.build();

releaseSoundpool = true;
Random r = new Random();
int Low = 0;
int High = 16;
int random = r.nextInt(High-Low) + Low;
final int soundId = soundPool.load(getApplicationContext(), sounds[random], 1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPool.play(soundId, 1, 1, 1, 0, 1);
}
});
Log.d("TouchscreenDashboard", "soundID played: " + random);
}
};
}

public void initializeTimerTask2() {

timerTask2 = new TimerTask() {
@Override
public void run() {
soundPool.release();
Log.d("TouchscreenDashboard", "SoundPool released");
}
};
}

我真的希望这对其他人有帮助。

关于java - 使用SoundPool、Timer和TimerTask每分钟播放一次随机声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60662360/

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