Reputation: 61
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
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
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