简体   繁体   中英

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?

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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM