0

我想从提示到屏幕开始播放声音,直到用户点击它的按钮,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();
4

1 回答 1

0

You can use SoundPool and then load and play as much sounds as you want:

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);
于 2013-09-11T10:28:36.213 回答