9

I'm trying to play an alarm sound through the speakers via the alarm channel at max volume. For that I'm using the AudioManager and a MediaPlayer. If I plug in headphones, the alarm is still played through the speakers, however the volume of the alarm played through the speakers decreases drastically making it useless for my purpose.

Is there a way to prevent this decrease in volume?

The code I'm using is this:

public void startAlarmSound() {
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setSpeakerphoneOn(false);

    audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);

    if (!alreadyPlaying)
        playAlarmSound();

    alreadyPlaying = true;
}

private void playAlarmSound() {
    mediaPlayer = new MediaPlayer();

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
        }
    });
    try {
        mediaPlayer.setDataSource(this, Uri.parse("android.resource://com.mystuff.mine/" + R.raw.alarm_sound));
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

To ensure that the volume has not been lowered, I'm calling the following every 5 seconds.

audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);

I'm located within the EU, so it could be caused by that regulation that deals with max volume when plugging in headphones. Since I only care about speaker output I need a workaround even if that is the case.

Edit:

This problem occurs both with my app as well as with system apps (like the alarm clock), and with both Nexus 5 and 6. As I've also read reports of that issue from other phone manufacturers, so I don't think the problem is exclusive to the nexus line of phones. I need a workaround.

I just checked the result of both getStreamMaxVolume(AudioManager.STREAM_ALARM) and getStreamVolume(AudioManager.STREAM_ALARM). Both display 7, regardless of if the headphones are plugged in or not.

I did notice that with headphones plugged in, while the volume indicator is set to max, if I reduce it and quickly increase it again, it will increase to the volume that it has without headphones. However as this requires user interaction, it's not the solution I'm looking for.

Syzygy
  • 507
  • 4
  • 21
  • Try to look here https://android.googlesource.com/platform/packages/apps/DeskClock/+/master/src/com/android/deskclock/AsyncRingtonePlayer.java#340 – John Apr 06 '17 at 12:17
  • Which specific part are you referring to? The only thing that seems like it could help is mediaPlayer.setVolume(1,1). I tried that but it didn't fix the problem. – Syzygy Apr 06 '17 at 13:25
  • The problem is that cannot increase volume of speaker only. But anyway try audioManager.requestAudioFocus(null, AudioManager.STREAM_ALARM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); – John Apr 06 '17 at 16:38
  • It's not a problem if headphone volume gets increased as well, but that EU regulation might pose a problem in that case. I'll check requestAudioFocus later today, but I'm fairly sure that that's among the things I've tried already - at least from the documentation it seems like it will not help (although the problem sounds very much like volume ducking described there.) – Syzygy Apr 07 '17 at 08:28
  • 2
    This is built in feature of devices, you cannot set volume so high until user will not allow it by himself as it can hurt ears, and the problem is that speaker and headphone volume is not separated – John Apr 07 '17 at 08:36

1 Answers1

2

According to john saying,

This is built in feature of devices, you cannot set volume so high until user will not allow it by himself as it can hurt ears, and the problem is that speaker and headphone volume is not separated

I think that you may not be able to make your volume full. I'dd suggest you trying to look if you can disable headphones (even if plugged in) then play the alarm (full volume as headphones are disabled), then re-enable headphone after alarm is shut down.

Take a look at this or this in order to disable headphones.

Community
  • 1
  • 1
Feuby
  • 670
  • 4
  • 7