改变警报的音量

时间:2016-11-17 14:28:41

标签: android media

我想为闹钟设置音量。我使用这段代码,但是卷似乎没有发生任何事情,只有日志显示我设置的卷的值。我该怎么做才能真正改变音量?

我的代码:

final MediaPlayer mediaPlayer = MediaPlayer.create(this, RingtoneManager.getActualDefaultRingtoneUri(this,RingtoneManager.TYPE_ALARM));

        final AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        final int currentVolume = audio.getStreamVolume(AudioManager.STREAM_ALARM);
        Log.e("Point_1", "Volume " + currentVolume);
        audio.setStreamVolume(AudioManager.STREAM_ALARM,0,0);
        mediaPlayer.start();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                mediaPlayer.stop();
                Log.e("Point_1", "Volume_after " + audio.getStreamVolume(AudioManager.STREAM_ALARM));
            }

    }, 5000);

感谢。

1 个答案:

答案 0 :(得分:0)

使用此代码

AudioManager audioManager = 
    (AudioManager)getSystemService(Context.AUDIO_SERVICE);

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                             [int value],
                             [if desired a flag]);

您应该使用AudioManager.STREAM_MUSIC代替AudioManager.STREAM_ALARM

并看到How to correctly set MediaPlayer audio stream type