在特定时间循环播放声音

时间:2013-09-11 10:09:17

标签: android loops audio alert android-alarms

我希望从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();

1 个答案:

答案 0 :(得分:0)

您可以使用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);