set Volume By Percent - Android Media

Android examples for Media:Audio

Description

set Volume By Percent

Demo Code


//package com.java2s;

import android.content.Context;

import android.media.AudioManager;

public class Main {
    public static void setVolumeByPercent(Context context, int value) {
        AudioManager mAudioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        int max = mAudioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int current = Math.round((float) max * (float) value / 100);

        mAudioManager/*from   w  ww  .  ja v  a  2  s.c  o m*/
                .setStreamVolume(AudioManager.STREAM_MUSIC, current, 0);
    }
}

Related Tutorials