Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speaker Volume (Alarm) decreases when Headphones are plugged in

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.

like image 379
Syzygy Avatar asked Mar 30 '17 09:03

Syzygy


People also ask

Why does my volume go down when I plug in my headphones?

An android device will require a specific electric signal for its volume buttons. Therefore, the kind of headphones and phone you are using may cause electrical differences, and this may make the phone think that the signal it is getting from the headphones is the volume down one, so it will lower the volume.

Will alarm sound if headphones are plugged in?

Modern smartphones have already installed alarm clock apps that can play sounds through any headphone, whether they may be wired or wireless headphones. If you set the alarm in your mobile phone system, the sound of the alarm will play through the headphones connected to your device and your phone's speakers.

Why are my headphones turning my sound down?

It's important that your headphone jack is clean. Any dirt, grime, or pocket lint can stick to the headphone jack which can cause interference with the audio signal, thus warping the sound or making it sound too quiet. Simply use a cloth or cotton bud damped with rubbing alcohol and wipe away any debris you see.


1 Answers

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.

like image 183
Feuby Avatar answered Nov 03 '22 20:11

Feuby