无法正确更改音量(setVolume)

时间:2019-07-06 08:00:37

标签: android android-mediaplayer

我正在开发一个需要在不同音量级别播放系统通知的应用程序,并且在使setVolume正常工作方面存在一些问题。

要调试此功能,我准备了一个带有两个按钮的示例应用程序,可让我以低音量或高音量播放系统通知。到目前为止,该应用程序有效,声音从扬声器发出。 但是,无论我按低音量和高音量按钮的顺序,一旦我以低音量播放通知,它都会保持低音,我无法让它再次以高音量播放。

下面是我的代码。我使用了oncompletion回调来增加计数器并发出3次通知。

public class MainActivity extends AppCompatActivity {

int soundsCounter = 0; // Counts how many times the notification has sounded
double playbackVolume = 1.0; // Playback volume of the notification
AudioManager audioManager;
MediaPlayer thePlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Setup the media player
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    thePlayer = MediaPlayer.create(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    // Initialize listener using setOnCompletionListener for mediaPlayer object
    // and declare new method OnComletionListener as an argument.
    thePlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        // Override onCompletion method to apply desired operations.
        @Override
        public void onCompletion(MediaPlayer mediaPlayer){
            soundsCounter++;
            if (soundsCounter < 3){ // Sound the notification 3 times
                playNotification();
            }

        }
    } );
}

// Plays system notification at the selected volume
private void playNotification() {
    try {
        thePlayer.setVolume(((float) (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) * playbackVolume)),
                (float) (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) * playbackVolume));
    }
    thePlayer.start();
}


// Called when the user taps the HighV button
public void playHighV(View view) {
    soundsCounter = 0;
    playbackVolume = 1.0;
    playNotification();
}


// Called when the user taps the LowV button
public void playLowV(View view) {
    soundsCounter = 0;
    playbackVolume = 0.02;
    playNotification();
}

任何想法在哪里看?谢谢!

1 个答案:

答案 0 :(得分:0)

感谢Oemel09,我意识到我在传递给setVolume的参数上犯了一些错误。

这是可以正常工作的新代码:

public class MainActivity extends AppCompatActivity {

int soundsCounter = 0; // Counts how many times the notification has sounded
float playbackVolume = 1; // Playback volume of the notification
AudioManager audioManager;
MediaPlayer thePlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Setup the media player
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    thePlayer = MediaPlayer.create(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    // Initialize listener using setOnCompletionListener for mediaPlayer object
    // and declare new method OnComletionListener as an argument.
    thePlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        // Override onCompletion method to apply desired operations.
        @Override
        public void onCompletion(MediaPlayer mediaPlayer){
            soundsCounter++;
            if (soundsCounter < 3){ // PLay sound 3 times
                playNotification();
            }

        }
    } );
}

// Plays system notification at the selected volume
private void playNotification() {
    thePlayer.setVolume(playbackVolume, playbackVolume);
    thePlayer.start();
}

// Called when the user taps the HighV button
public void playHighV(View view) {
    soundsCounter = 0;
    playbackVolume = 1f;
    playNotification();
}

// Called when the user taps the LowV button
public void playLowV(View view) {
    soundsCounter = 0;
    playbackVolume = 0.1f;
    playNotification();
}

}