Mimmo
Mimmo

Reputation: 61

Android : setVolume and setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

I am playing an audio file with an internal speaker using this code

audioManager = (AudioManager)Context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(false);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

How can I set the volume?

Upvotes: 1

Views: 10047

Answers (2)

jroal
jroal

Reputation: 557

am2 is an instance of AudioManager system service. am2 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

// makes the media volume adjustment
public static int setVolume(int inputVol, Context sender) {
    int outVol;
    if (inputVol < 0)
        inputVol = 0;
    if (inputVol > am2.getStreamMaxVolume(AudioManager.STREAM_MUSIC))
        inputVol = am2.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    am2.setStreamVolume(AudioManager.STREAM_MUSIC, inputVol,
            AudioManager.FLAG_SHOW_UI);
    outVol = am2.getStreamVolume(AudioManager.STREAM_MUSIC);
    return outVol;
}

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007349

Use adjustStreamVolume() on AudioManager.

Though, preferably, you let the user set the volume the normal way, via the volume control buttons. You can indicate what stream that is to control in your activity via setVolumeControlStream().

Upvotes: 2

Related Questions