Android修改音量

1.必须要获取系统的声音服务权限

2.声明控制声音的变量
//通话音量
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

3.通过AudoManager的get和set方法来获得声音以及设置声音了
3.1
//获取提示声音音量 
current = mAudioManager.getStreamVolume(AudioManager.STREAM_RING ); 

//获取音乐音量  
current = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC );  
其中AudioManager.STREAM_MUSIC代表媒体音量,也可以替换成其他的类型,获取其他类型音量。

3.2//设置音量函数  
public void setStreamVolume(int streamType, int index, int flags)
     * 参数说明: 
     * streamType为铃声类型,例如:AudioManager.STREAM_VOICE_CALL、AudioManager.STREAM_SYSTEM等,
     * 
     * index为音量大小
     * 
     * falgs为标志位,一般设置成0就可以了,感觉无影响 
     */
     
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);

//通话音量

int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
int current = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
Log.d("VIOCE_CALL", "max : " + max + " current : " + current);

//系统音量

max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM );
Log.d("SYSTEM", "max : " + max + " current : " + current);

//铃声音量

max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );
Log.d("RING", "max : " + max + " current : " + current);

//音乐音量

max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
Log.d("MUSIC", "max : " + max + " current : " + current);

//提示声音音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_ALARM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_ALARM );
Log.d("ALARM", "max : " + max + " current : " + current);
ps:
  游戏过程中只允许调整多媒体音量,而不允许调整通话音量。
  setVolumeControlStream(AudioManager.STREAM_MUSIC);
  长时间不动,不允许黑屏,View.setKeepScreenOn(true);
  估计manifest文件中需要注册权限吧

 

4.调节媒体音量
AudioManager audio = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
重写 Activity 的 onKeyDown 方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(
            AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_RAISE,
            AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(
            AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_LOWER,
            AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        break;
    }
    return super.onKeyDown(keyCode, event);
}

5.监听音量变化:
上面通过监听按键来监听调整音量的方法,不是很靠谱,因为可能用户在设置里调整音量,所以用下面方式监听音量变化

private void registerVolumeChangeReceiver() {
    mSettingsContentObserver = new SettingsContentObserver(mContext, new Handler());
    mContext.getContentResolver()
            .registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);
}

private void unregisterVolumeChangeReceiver() {
    mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
}

public class SettingsContentObserver extends ContentObserver {
    Context context;

    public SettingsContentObserver(Context c, Handler handler) {
        super(handler);
        context = c;
    }

    @Override
    public boolean deliverSelfNotifications() {
        return super.deliverSelfNotifications();
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        Log.d(TAG, "音量:" + currentVolume);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);
    }
}

6.监听震动模式变化
public boolean isMuteMode() {
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    return am.getRingerMode() == AudioManager.RINGER_MODE_SILENT;
}

public boolean isVibrateMode() {
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    return am.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE;
}

public boolean isNormalMode() {
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    return am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
}

private void registerRingerModeReceiver() {
    mRingerModeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
                // 监听到震动/静音/响铃的模式变化

                  if( isMuteMode()){

                 }
            }
        }
    };
    IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
    mContext.registerReceiver(mRingerModeReceiver, filter);
}
 
 

你可能感兴趣的:(Android)