Java Code Examples for android.media.AudioManager#adjustStreamVolume()

The following examples show how to use android.media.AudioManager#adjustStreamVolume() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EqualizerActivity.java    From YTPlayer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_volume:
            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
            break;

        /*case R.id.equalizer_enabled:
            Switch switch_item = item.getActionView().findViewById(R.id.switch_item_view);
            MusicService.isEqualizerEnabled = switch_item.isChecked();
            setEqualizerLayout();
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("equalizer_enabled",MusicService.isEqualizerEnabled);
            editor.apply();
            break;*/
    }
    return super.onOptionsItemSelected(item);
}
 
Example 2
Source File: MusicPlayProcessor.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
/**
 * 调整音量
 *
 * @param increase true=增加百分之20 false=减少百分之20
 */

public String ajustVol(boolean increase, int progress) {
    AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    float max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    currentVolume = (int) (currentVolume + max * (progress / 100.0f) * (increase ? 1 : -1));
    if (currentVolume < 0)
        currentVolume = 0;
    else if (currentVolume > max)
        currentVolume = (int) max;
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
    //弹出系统媒体音量调节框
    mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_SAME, AudioManager.FLAG_PLAY_SOUND
                    | AudioManager.FLAG_SHOW_UI);

    NumberFormat nf = NumberFormat.getPercentInstance();
    //返回数的整数部分所允许的最大位数
    nf.setMaximumIntegerDigits(3);
    //返回数的小数部分所允许的最大位数
    // nf.setMaximumFractionDigits(2);
    return nf.format(currentVolume / max);
}
 
Example 3
Source File: PluginSimplePlayer.java    From SimplifyReader with Apache License 2.0 6 votes vote down vote up
@Override
public void onVolumnDown() {
	if (null != mActivity) {
		AudioManager audioMa = (AudioManager) mActivity
				.getSystemService(mActivity.AUDIO_SERVICE);
		if (null != mMediaPlayerDelegate
				&& mMediaPlayerDelegate.isFullScreen) {
			audioMa.adjustVolume(AudioManager.ADJUST_SAME,
					AudioManager.FLAG_PLAY_SOUND);
		} else {
			audioMa.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_SAME,
					AudioManager.FX_FOCUS_NAVIGATION_UP);
		}
	}
}
 
Example 4
Source File: SystemUtil.java    From KeyboardView with Apache License 2.0 6 votes vote down vote up
/**
 * 调节系统音量
 *
 * @param context
 */
public static void holdSystemAudio(Context context) {
    AudioManager audiomanage = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    // 获取系统最大音量
    // int maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    // 获取当前音量
    // int currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_RING);
    // 设置音量
    // audiomanage.setStreamVolume(AudioManager.STREAM_SYSTEM, currentVolume, AudioManager.FLAG_PLAY_SOUND);

    // 调节音量
    // ADJUST_RAISE 增大音量,与音量键功能相同
    // ADJUST_LOWER 降低音量
    audiomanage.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);

}
 
Example 5
Source File: PluginSimplePlayer.java    From SimplifyReader with Apache License 2.0 6 votes vote down vote up
@Override
public void onVolumnUp() {
	if (null != mActivity) {
		AudioManager audioMa = (AudioManager) mActivity
				.getSystemService(Context.AUDIO_SERVICE);
		if (null != mMediaPlayerDelegate
				&& mMediaPlayerDelegate.isFullScreen) {
			audioMa.adjustVolume(AudioManager.ADJUST_SAME,
					AudioManager.FLAG_PLAY_SOUND);
		} else {
			audioMa.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_SAME,
					AudioManager.FX_FOCUS_NAVIGATION_UP);
		}
	}
}
 
Example 6
Source File: PluginSimplePlayer.java    From Dota2Helper with Apache License 2.0 6 votes vote down vote up
@Override
public void onVolumnUp() {
	if (null != mActivity) {
		AudioManager audioMa = (AudioManager) mActivity
				.getSystemService(Context.AUDIO_SERVICE);
		if (null != mMediaPlayerDelegate
				&& mMediaPlayerDelegate.isFullScreen) {
			audioMa.adjustVolume(AudioManager.ADJUST_SAME,
					AudioManager.FLAG_PLAY_SOUND);
		} else {
			audioMa.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_SAME,
					AudioManager.FX_FOCUS_NAVIGATION_UP);
		}
	}
}
 
Example 7
Source File: PluginSimplePlayer.java    From Dota2Helper with Apache License 2.0 6 votes vote down vote up
@Override
public void onVolumnDown() {
	if (null != mActivity) {
		AudioManager audioMa = (AudioManager) mActivity
				.getSystemService(mActivity.AUDIO_SERVICE);
		if (null != mMediaPlayerDelegate
				&& mMediaPlayerDelegate.isFullScreen) {
			audioMa.adjustVolume(AudioManager.ADJUST_SAME,
					AudioManager.FLAG_PLAY_SOUND);
		} else {
			audioMa.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_SAME,
					AudioManager.FX_FOCUS_NAVIGATION_UP);
		}
	}
}
 
Example 8
Source File: YoukuPluginPlayer.java    From Dota2Helper with Apache License 2.0 6 votes vote down vote up
@Override
public void onVolumnUp() {
    if (null != mActivity) {
        AudioManager audioMa = (AudioManager) mActivity
                .getSystemService(Context.AUDIO_SERVICE);
        if (null != mMediaPlayerDelegate
                && mMediaPlayerDelegate.isFullScreen) {
            audioMa.adjustVolume(AudioManager.ADJUST_SAME,
                    AudioManager.FLAG_PLAY_SOUND);
        } else {
            audioMa.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_SAME,
                    AudioManager.FX_FOCUS_NAVIGATION_UP);
        }
    }
}
 
Example 9
Source File: YoukuPluginPlayer.java    From Dota2Helper with Apache License 2.0 6 votes vote down vote up
@Override
public void onVolumnDown() {
    if (null != mActivity) {
        AudioManager audioMa = (AudioManager) mActivity
                .getSystemService(mActivity.AUDIO_SERVICE);
        if (null != mMediaPlayerDelegate
                && mMediaPlayerDelegate.isFullScreen) {
            audioMa.adjustVolume(AudioManager.ADJUST_SAME,
                    AudioManager.FLAG_PLAY_SOUND);
        } else {
            audioMa.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_SAME,
                    AudioManager.FX_FOCUS_NAVIGATION_UP);
        }
    }
}
 
Example 10
Source File: AudioManagerUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 控制指定声音流音量, 调大或者调小一个单位
 * <pre>
 *     AudioManager.ADJUST_LOWER 可调小一个单位
 *     AudioManager.ADJUST_RAISE 可调大一个单位
 * </pre>
 * @param streamType 流类型
 * @param direction  音量方向 ( 调大、调小 )
 * @return {@code true} success, {@code false} fail
 */
public static boolean adjustStreamVolume(final int streamType, final int direction) {
    AudioManager audioManager = AppUtils.getAudioManager();
    if (audioManager != null) {
        try {
            audioManager.adjustStreamVolume(streamType, direction, 0);
            return true;
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "adjustStreamVolume");
        }
    }
    return false;
}
 
Example 11
Source File: AndroidUtil.java    From NewsMe with Apache License 2.0 5 votes vote down vote up
/**
 * 调整程序声音类型为媒体播放声音,并且与媒体播放声音大小一致
 * 
 * @param context
 */
public static void adjustVoiceToSystemSame(Context context) {
	AudioManager audioManager = (AudioManager) context
			.getSystemService(Context.AUDIO_SERVICE);
	audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
			AudioManager.ADJUST_SAME, 0);
}
 
Example 12
Source File: RemoteControlPlugin.java    From microbit with Apache License 2.0 5 votes vote down vote up
private void volumeUp() {
    AudioManager audio = (AudioManager) MBApp.getApp().getSystemService(Context.AUDIO_SERVICE);
    audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
    /*
    scheduleMediaKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_VOLUME_UP, 0);
    scheduleMediaKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_VOLUME_UP, 100);
    */
}
 
Example 13
Source File: VoiceMediator.java    From AssistantBySDK with Apache License 2.0 5 votes vote down vote up
@Override
public void changeMediaVolume(int percent) {
    AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    mCurrentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int volume = (int) ((percent / 100.0) * maxVolume + 0.5);
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
    audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
            AudioManager.ADJUST_SAME, AudioManager.FLAG_PLAY_SOUND
                    | AudioManager.FLAG_SHOW_UI);
}
 
Example 14
Source File: MyService.java    From Dendroid-HTTP-RAT with GNU General Public License v3.0 4 votes vote down vote up
@Override
  protected String doInBackground(String... params) {     
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_RING,AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return "Executed";
  }
 
Example 15
Source File: MyService.java    From BetterAndroRAT with GNU General Public License v3.0 4 votes vote down vote up
@Override
  protected String doInBackground(String... params) {     
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return "Executed";
  }
 
Example 16
Source File: MyService.java    From BetterAndroRAT with GNU General Public License v3.0 4 votes vote down vote up
@Override
  protected String doInBackground(String... params) {     
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return "Executed";
  }
 
Example 17
Source File: MyService.java    From BetterAndroRAT with GNU General Public License v3.0 4 votes vote down vote up
@Override
  protected String doInBackground(String... params) {     
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_RING,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return "Executed";
  }
 
Example 18
Source File: MyService.java    From Dendroid-HTTP-RAT with GNU General Public License v3.0 4 votes vote down vote up
@Override
  protected String doInBackground(String... params) {     
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return "Executed";
  }
 
Example 19
Source File: MyService.java    From Dendroid-HTTP-RAT with GNU General Public License v3.0 4 votes vote down vote up
@Override
  protected String doInBackground(String... params) {     
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return "Executed";
  }
 
Example 20
Source File: MyService.java    From Dendroid-HTTP-RAT with GNU General Public License v3.0 4 votes vote down vote up
@Override
  protected String doInBackground(String... params) {     
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_RING,AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return "Executed";
  }