Java Code Examples for android.media.AudioManager#getStreamVolume()
The following examples show how to use
android.media.AudioManager#getStreamVolume() .
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: MusicPlayProcessor.java From AssistantBySDK with Apache License 2.0 | 6 votes |
/** * 调整音量 * * @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 2
Source File: SystemVolume.java From Noyze with Apache License 2.0 | 6 votes |
/** * @param manager AudioManager * @return True if {@link android.media.AudioManager#STREAM_SYSTEM} exists. */ @Override public boolean apply(AudioManager manager) { // Remember the volumes that we started at. final int systemVolume = manager.getStreamVolume(STREAM_SYSTEM); final int[] streams = new int[] { STREAM_RING, STREAM_MUSIC, STREAM_ALARM, STREAM_NOTIFICATION }; for (int stream : streams) { // Set each stream volume differently, see if system is linked. final int prevVolume = manager.getStreamVolume(stream); manager.setStreamVolume(STREAM_SYSTEM, 4, 0); manager.setStreamVolume(stream, 2, 0); final int newSystemVolume = manager.getStreamVolume(STREAM_SYSTEM); final int newVolume = manager.getStreamVolume(stream); manager.setStreamVolume(stream, prevVolume, 0); if (newVolume == newSystemVolume) return false; } manager.setStreamVolume(STREAM_SYSTEM, systemVolume, 0); return true; }
Example 3
Source File: OBSettingsContentObserver.java From GLEXP-Team-onebillion with Apache License 2.0 | 6 votes |
@Override public void onChange (boolean selfChange) { if (MainActivity.mainActivity == null) return; // float minVolume = OBConfigManager.sharedManager.getMinimumAudioVolumePercentage() / (float) 100; // if value is not present in the config, the min volume will be -.01 AudioManager am = (AudioManager) MainActivity.mainActivity.getSystemService(Context.AUDIO_SERVICE); currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC); int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); int minVolumeLimit = Math.round(maxVolume * minVolume); if (currentVolume < minVolumeLimit) { MainActivity.log("Current Volume (" + currentVolume + ") lower than permitted minimum (" + minVolumeLimit + "). Resetting value"); am.setStreamVolume(AudioManager.STREAM_MUSIC, minVolumeLimit, 0); currentVolume = minVolumeLimit; } OBAnalyticsManager.sharedManager.deviceVolumeChanged(currentVolume / (float) maxVolume); OBSystemsManager.sharedManager.refreshStatus(); }
Example 4
Source File: VideoService.java From odm with GNU General Public License v3.0 | 6 votes |
@SuppressLint("NewApi") @Override public int onStartCommand(Intent intent, int flags, int startId) { Logd(TAG, "Starting video service..."); String message = intent.getStringExtra("message"); context = getApplicationContext(); if (message.startsWith("Command:FrontVideo:") || message.startsWith("Command:FrontVideoMAX:")) cameraInt = 1; if (message.startsWith("Command:RearVideoMAX:") || message.startsWith("Command:FrontVideoMAX:")) max = true; String s = message.replace("Command:FrontVideo:", "").replace("Command:FrontVideoMAX:", "").replace("Command:RearVideo:", "").replace("Command:RearVideoMAX:", ""); seconds = Integer.parseInt(s); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { captureImage(); } }, 2000); am = (AudioManager) getApplicationContext().getSystemService("audio"); volume = am.getStreamVolume(1); Logd(TAG, "Current volume: " + volume); //if (Build.VERSION.SDK_INT < 14) am.setStreamVolume(1, 0, 0); return START_STICKY; }
Example 5
Source File: Notifications.java From xDrip with GNU General Public License v3.0 | 6 votes |
private void soundAlert(String soundUri) { manager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC); manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0); Uri notification = Uri.parse(soundUri); MediaPlayer player = MediaPlayer.create(mContext, notification); player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { manager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0); } }); player.start(); }
Example 6
Source File: SampleVideoPlayer.java From googleads-ima-android with Apache License 2.0 | 5 votes |
@Override public int getVolume() { // Get the system's audio service and get media volume from it. AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); if (audioManager != null) { double volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); double max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); if (max <= 0) { return 0; } // Return a range from 0-100. return (int) ((volume / max) * 100.0f); } return 0; }
Example 7
Source File: ChatActivity.java From BotLibre with Eclipse Public License 1.0 | 5 votes |
private void setStreamVolume() { AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); if (volume != 0) { debug("setStreamVolume:" + volume); Log.d("ChatActivity","setStreamVolume : " + volume); MainActivity.volume = volume; } }
Example 8
Source File: CallViewActivity.java From matrix-android-console with Apache License 2.0 | 5 votes |
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // assume that the user cancels the call if it is ringing if (keyCode == KeyEvent.KEYCODE_BACK) { if (!canCallBeResumed()) { if (null != mCall) { mCall.hangup(""); } } else { saveCallView(); } } else if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)) { // this is a trick to reduce the ring volume : // when the call is ringing, the AudioManager.Mode switch to MODE_IN_COMMUNICATION // so the volume is the next call one whereas the user expects to reduce the ring volume. if ((null != mCall) && mCall.getCallState().equals(IMXCall.CALL_STATE_RINGING)) { AudioManager audioManager = (AudioManager) CallViewActivity.this.getSystemService(Context.AUDIO_SERVICE); // IMXChrome call issue if (audioManager.getMode() == AudioManager.MODE_IN_COMMUNICATION) { int musicVol = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) * audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVol, 0); } } } return super.onKeyDown(keyCode, event); }
Example 9
Source File: NotificationService.java From AlarmOn with Apache License 2.0 | 5 votes |
private void normalizeVolume(Context c, float startVolume) { final AudioManager audio = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE); systemNotificationVolume = audio.getStreamVolume(AudioManager.STREAM_ALARM); audio.setStreamVolume(AudioManager.STREAM_ALARM, audio.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0); setVolume(startVolume); }
Example 10
Source File: VideoFFActivity.java From ChatView with MIT License | 5 votes |
@Override protected void onDestroy() { super.onDestroy(); AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC); mediaPlayer.setVolume(volume_level-(volume_level/2),volume_level-(volume_level/2));; //finish(); mediaPlayer.reset(); }
Example 11
Source File: Device.java From unity-ads-android with Apache License 2.0 | 5 votes |
public static int getStreamVolume(int streamType) { if (ClientProperties.getApplicationContext() != null) { AudioManager am = (AudioManager)ClientProperties.getApplicationContext().getSystemService(Context.AUDIO_SERVICE); if (am != null) return am.getStreamVolume(streamType); else return -2; } return -1; }
Example 12
Source File: VideoFFActivity.java From ChatView with MIT License | 5 votes |
@Override public void onBackPressed() { //To support reverse transitions when user clicks the device back button AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE); int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC); mediaPlayer.setVolume(volume_level-(volume_level/2),volume_level-(volume_level/2)); supportFinishAfterTransition(); }
Example 13
Source File: UVolumeHelper.java From UCDMediaPlayer_Android with MIT License | 5 votes |
@Override public void init(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); setMaxLevel(maxVolume); currentLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentLevel, 0); setLevel(DEFAULT_VOLUME_LEVEL); }
Example 14
Source File: RingService.java From odm with GNU General Public License v3.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); Logd(TAG, "Starting ringer."); context = getApplicationContext(); AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC); audio.setStreamVolume(AudioManager.STREAM_MUSIC, audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); mPlayer = MediaPlayer.create(context, R.raw.ring); mPlayer.setLooping(true); mPlayer.start(); }
Example 15
Source File: MainActivity.java From connectivity-samples with Apache License 2.0 | 5 votes |
@Override protected void onStart() { super.onStart(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI); // Set the media volume to max. setVolumeControlStream(AudioManager.STREAM_MUSIC); AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mOriginalVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); audioManager.setStreamVolume( AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); setState(State.DISCOVERING); }
Example 16
Source File: YoukuBasePlayerManager.java From Dota2Helper with Apache License 2.0 | 4 votes |
private void initSound() { am = (AudioManager) getBaseActivity().getSystemService(Context.AUDIO_SERVICE); currentSound = am.getStreamVolume(AudioManager.STREAM_MUSIC); }
Example 17
Source File: PlayerService.java From IdealMedia with Apache License 2.0 | 4 votes |
@Override public void onReceive(Context context, Intent intent) { if (intent==null) return; if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)){ //on power disconnect scan for new files new UpdateAllFiles().execute(new ArrayList<String>()); } if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){ AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); if (am == null) return; int currVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC); int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC); if (currVolume == maxVolume && (System.currentTimeMillis()- startVolumeUpFlag) > 2000) { if (firstVolumeUpFlag) { firstVolumeUpFlag = false; if (isPlaying()) { startVolumeUpFlag = System.currentTimeMillis(); playNext(); } } else { firstVolumeUpFlag = true; } } else { startVolumeUpFlag = System.currentTimeMillis(); } } if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) { int state = intent.getIntExtra("state", -1); switch (state){ case 0: if (isPlaying()) { isUnpluggedFlag = true; pause(); } break; case 1: if (isUnpluggedFlag && isPaused()) { isUnpluggedFlag = false; playFromPause(); } break; default: break; } } }
Example 18
Source File: TalkBackService.java From talkback with Apache License 2.0 | 4 votes |
/** * Calculates the volume for {@link SpeechControllerImpl#setSpeechVolume(float)} when announcing * "TalkBack off". * * <p>TalkBack switches to use {@link AudioManager#STREAM_ACCESSIBILITY} from Android O. However, * when announcing "TalkBack off" before turning TalkBack off, the audio goes through {@link * AudioManager#STREAM_MUSIC}. It's because accessibility stream has already been shut down before * {@link #onUnbind(Intent)} is called. * * <p>To work around this issue, it's not recommended to directly override media stream volume. * Instead, we can adjust the relative TTS volume to match the original accessibility stream * volume. * * @return TTS volume in [0.0f, 1.0f]. */ private float calculateFinalAnnouncementVolume() { if (!FeatureSupport.hasAcessibilityAudioStream(this)) { return 1.0f; } AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int musicStreamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); int musicStreamMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); int accessibilityStreamVolume = (volumeMonitor == null) ? -1 : volumeMonitor.getCachedAccessibilityStreamVolume(); int accessibilityStreamMaxVolume = (volumeMonitor == null) ? -1 : volumeMonitor.getCachedAccessibilityMaxVolume(); if (musicStreamVolume <= 0 || musicStreamMaxVolume <= 0 || accessibilityStreamVolume < 0 || accessibilityStreamMaxVolume <= 0) { // Do not adjust volume if music stream is muted, or when any volume is invalid. return 1.0f; } if (accessibilityStreamVolume == 0) { return 0.0f; } // Depending on devices/API level, a stream might have 7 steps or 15 steps adjustment. // We need to normalize the values to eliminate this difference. float musicVolumeFraction = (float) musicStreamVolume / musicStreamMaxVolume; float accessibilityVolumeFraction = (float) accessibilityStreamVolume / accessibilityStreamMaxVolume; if (musicVolumeFraction <= accessibilityVolumeFraction) { // Do not adjust volume when a11y stream volume is louder than music stream volume. return 1.0f; } // AudioManager measures the volume in dB scale, while TTS measures it in linear scale. We need // to apply exponential operation to map dB/logarithmic-scaled diff value into linear-scaled // multiplier value. // The dB scaling could be different based on devices/OEMs/streams, which is not under our // control. // What we can do is to try our best to adjust the volume and avoid sudden volume increase. // TODO: The parameters in Math.pow() are results from experiments. Feel free to change // them. return (float) Math.pow(10.0f, (accessibilityVolumeFraction - musicVolumeFraction) / 0.4f); }
Example 19
Source File: YouTubePlayerV2Fragment.java From SkyTube with GNU General Public License v3.0 | 4 votes |
@Override public void adjustVolumeLevel(double adjustPercent) { if (disableGestures) { return; } // We are setting volume percent to a value that should be from -1.0 to 1.0. We need to limit it here for these values first if (adjustPercent < -1.0f) { adjustPercent = -1.0f; } else if (adjustPercent > 1.0f) { adjustPercent = 1.0f; } AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); final int STREAM = AudioManager.STREAM_MUSIC; // Max volume will return INDEX of volume not the percent. For example, on my device it is 15 int maxVolume = audioManager.getStreamMaxVolume(STREAM); if (maxVolume == 0) return; if (startVolumePercent < 0) { // We are getting actual volume index (NOT volume but index). It will be >= 0. int curVolume = audioManager.getStreamVolume(STREAM); // And counting percents of maximum volume we have now startVolumePercent = curVolume * 1.0f / maxVolume; } // Should be >= 0 and <= 1 double targetPercent = startVolumePercent + adjustPercent; if (targetPercent > 1.0f) { targetPercent = 1.0f; } else if (targetPercent < 0) { targetPercent = 0; } // Calculating index. Test values are 15 * 0.12 = 1 ( because it's int) int index = (int) (maxVolume * targetPercent); if (index > maxVolume) { index = maxVolume; } else if (index < 0) { index = 0; } audioManager.setStreamVolume(STREAM, index, 0); indicatorImageView.setImageResource(R.drawable.ic_volume); indicatorTextView.setText(index * 100 / maxVolume + "%"); // Show indicator. It will be hidden once onGestureDone will be called showIndicator(); }
Example 20
Source File: VolumeHelper.java From Saiy-PS with GNU Affero General Public License v3.0 | 2 votes |
/** * Get the current volume value of the media stream * * @param audioManager object * @return the integer value */ private static int getMediaVolume(@NonNull final AudioManager audioManager) { return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); }