0

I'm working on an app that needs to play the system notification at different volume levels and have some issues with getting setVolume to work properly.

To debug this, I prepared a sample app with two buttons that allows me to play the system notification at a low or a high volume. So far, the app works, and the sound comes out of the speaker. However, no matter in which sequence I press the low and high volume buttons, once I have played the notification at a low volume, it stays low, I can't get it to play at the higher volume again.

Below is my code. I used an oncompletion callback to increment a counter and sound the notification 3 times.

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();
}

Any ideas where to look? Thanks!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
kxtronic
  • 331
  • 5
  • 16
  • Check out this post: https://stackoverflow.com/questions/5215459/android-mediaplayer-setvolume-function – oemel09 Jul 06 '19 at 09:16

1 Answers1

0

Thanks to Oemel09 I realized I was making some mistakes in the parameters passed to setVolume.

Here's the new code that works fine:

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();
}

}

kxtronic
  • 331
  • 5
  • 16