gpt4 book ai didi

android - 在特定时间循环播放声音

转载 作者:行者123 更新时间:2023-12-03 02:17:09 24 4
gpt4 key购买 nike

我想从屏幕上提示alertDialog时开始播放声音
直到用户单击它的按钮,

每隔x秒它将播放另一种声音。
(类似于一般警报,但声音不断变化)

我尝试使用mediaPlayer,但不知道如何播放声音。

我该怎么做 ?
这是我的警报代码:

 AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(MainActivity.this);
//Setting Dialog noise
mediaPlayer = MediaPlayer.create(this, R.raw.house_fire_alarm);
mediaPlayer.setLooping(true);
mediaPlayer.start();

// Setting Dialog vibrate
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern= {100, 1000};
vibrator.vibrate( pattern ,0);
// Setting Dialog Title
alertDialog3.setTitle("reminder:");

// Setting Dialog Message
alertDialog3.setMessage("dont forget");

// Setting Icon to Dialog
alertDialog3.setIcon(R.drawable.ic_launcher);

// Setting Cancelable
alertDialog3.setCancelable(false);

// Setting Positive Button
alertDialog3.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.release();
}
mediaPlayer = null;

if (vibrator.hasVibrator()){
vibrator.cancel();
vibrator = null;
}
......
finish();
}
});
// Setting Negative Button
alertDialog3.setNegativeButton("Rate us", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke RateUs event
if (mediaPlayer.isPlaying()) {
mediaPlayer.release();
}
mediaPlayer = null;

if (vibrator.hasVibrator()){
vibrator.cancel();
vibrator = null;
}

//open browser in activity to app page in google play
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName() ) ) );
}
});

// Showing Alert Message
alertDialog3.show();

最佳答案

您可以使用SoundPool,然后根据需要加载和播放尽可能多的声音:

SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
int sound1 = soundPool.load(this, R.raw.house_fire_alarm, 1);
int sound2 = soundPool.load(this, R.raw.house_fire_alarm1, 1);
....
soundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);

关于android - 在特定时间循环播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18738315/

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