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

The following examples show how to use android.media.AudioManager#setStreamVolume() . 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: MainActivity.java    From connectivity-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onStop() {
  mSensorManager.unregisterListener(this);

  // Restore the original volume.
  AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mOriginalVolume, 0);
  setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);

  if (isRecording()) {
    stopRecording();
  }
  if (isPlaying()) {
    stopPlaying();
  }

  setState(State.UNKNOWN);

  mUiHandler.removeCallbacksAndMessages(null);

  if (mCurrentAnimator != null && mCurrentAnimator.isRunning()) {
    mCurrentAnimator.cancel();
  }

  super.onStop();
}
 
Example 2
Source File: ActivityCallViewModel.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
private void muteMusic() {

        if (!isMuteAllMusic) {
            AudioManager am = (AudioManager) G.context.getSystemService(Context.AUDIO_SERVICE);
            if (am == null) {
                return;
            }
            int result = am.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
                @Override
                public void onAudioFocusChange(int focusChange) {

                }
            }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
            if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                musicVolum = am.getStreamVolume(AudioManager.STREAM_MUSIC);
                am.setStreamMute(AudioManager.STREAM_MUSIC, true);
                am.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
                isMuteAllMusic = true;
            }
        }
    }
 
Example 3
Source File: OBSettingsContentObserver.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
@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: AlarmScreenActivity.java    From BaldPhone with Apache License 2.0 6 votes vote down vote up
public static Ringtone getRingtone(Context context) {
    Uri alert =
            RingtoneManager
                    .getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_ALARM);
    if (alert == null)
        alert = RingtoneManager
                .getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_NOTIFICATION);
    if (alert == null)
        alert = RingtoneManager
                .getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
    final Ringtone ringtone = RingtoneManager.getRingtone(context, alert);
    final AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
    if (audioManager != null) {//who knows lol - btw don't delete user's may lower the alarm sounds by mistake
        final int alarmVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM) * (BPrefs.get(context).getInt(BPrefs.ALARM_VOLUME_KEY, BPrefs.ALARM_VOLUME_DEFAULT_VALUE) + 6) / 10;
        audioManager.setStreamVolume(AudioManager.STREAM_ALARM, alarmVolume, 0);
    }
    ringtone.setAudioAttributes(alarmAttributes);
    return ringtone;
}
 
Example 5
Source File: SystemVolume.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * @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 6
Source File: RingerModeTransition.java    From Noyze with Apache License 2.0 6 votes vote down vote up
/**
 * @return An ordered list of the ringer modes, from
 * highest to lowest (volumes [1], [0], [0] again).
 */
public int[] apply(AudioManager manager) {
    // The algorithm here is to go from volume 0 (again) => 0 => 1,
    // and with each change, record the ringer modes.
    final int[] MODES = new int[3];
    MODES[0] = RINGER_MODE_NORMAL;
    final int STREAM = STREAM_RING;
    final int MODE = manager.getRingerMode();
    final int startVolume = manager.getStreamVolume(STREAM);
    // API quirk: volume must be decremented from 1 to get ringer mode change
    manager.setStreamVolume(STREAM, 1, FLAG_SHOW_UI);
    manager.setRingerMode(RINGER_MODE_NORMAL);
    manager.adjustStreamVolume(STREAM, ADJUST_LOWER, FLAG_SHOW_UI & FLAG_ALLOW_RINGER_MODES);
    manager.adjustStreamVolume(STREAM, ADJUST_LOWER, FLAG_SHOW_UI & FLAG_ALLOW_RINGER_MODES);
    MODES[2] = manager.getRingerMode();
    manager.adjustStreamVolume(STREAM, ADJUST_RAISE, FLAG_SHOW_UI & FLAG_ALLOW_RINGER_MODES);
    MODES[1] = manager.getRingerMode();
    // There are two possible ways the device may work. It may have a silent/vibrate
    // mode or it may have distinct silent and vibrate modes.
    manager.setStreamVolume(STREAM, startVolume, 0);
    manager.setRingerMode(MODE);
    return MODES;
}
 
Example 7
Source File: PlayerService.java    From IdealMedia with Apache License 2.0 5 votes vote down vote up
public void volumeUp() {
    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);
    currVolume = currVolume +(maxVolume/10);
    if (currVolume > maxVolume)
        currVolume = maxVolume;

    am.setStreamVolume(AudioManager.STREAM_MUSIC, currVolume, AudioManager.FLAG_SHOW_UI);
}
 
Example 8
Source File: OperationManager.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Mute the device.
 *
 * @param operation - Operation object.
 */
public void muteDevice(org.wso2.emm.agent.beans.Operation operation) {
    operation.setStatus(resources.getString(R.string.operation_value_completed));
    resultBuilder.build(operation);

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_RING, DEFAULT_VOLUME, DEFAULT_FLAG);
    if (Constants.DEBUG_MODE_ENABLED) {
        Log.d(TAG, "Device muted");
    }
}
 
Example 9
Source File: HostSettingProfile.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public boolean onRequest(final Intent request, final Intent response) {
    VolumeKind kind = getVolumeKind(request);
    Double level = getVolumeLevel(request);

    AudioManager manager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
    double maxVolume = 1;
    if (kind == VolumeKind.ALARM) {
        maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
        manager.setStreamVolume(AudioManager.STREAM_ALARM, (int) (maxVolume * level), 1);
        setResult(response, DConnectMessage.RESULT_OK);
    } else if (kind == VolumeKind.CALL) {
        maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
        manager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, (int) (maxVolume * level), 1);
        setResult(response, DConnectMessage.RESULT_OK);
    } else if (kind == VolumeKind.RINGTONE) {
        maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_RING);
        manager.setStreamVolume(AudioManager.STREAM_RING, (int) (maxVolume * level), 1);
        setResult(response, DConnectMessage.RESULT_OK);
    } else if (kind == VolumeKind.MAIL) {
        maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_RING);
        manager.setStreamVolume(AudioManager.STREAM_RING, (int) (maxVolume * level), 1);
        setResult(response, DConnectMessage.RESULT_OK);
    } else if (kind == VolumeKind.MEDIA_PLAYER) {
        maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        manager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) (maxVolume * level), 1);
        setResult(response, DConnectMessage.RESULT_OK);
    } else if (kind == VolumeKind.OTHER) {
        MessageUtils.setNotSupportAttributeError(response, "volume type is not support.");
    } else {
        MessageUtils.setInvalidRequestParameterError(response, "type is invalid.");
    }
    return true;
}
 
Example 10
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 11
Source File: AudioUtil.java    From CameraView with Apache License 2.0 5 votes vote down vote up
public static void setAudioManage(Context context){
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
    audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
    audioManager.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
    audioManager.setStreamVolume(AudioManager.STREAM_DTMF, 0, 0);
    audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, 0);
    audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
}
 
Example 12
Source File: OBSettingsContentObserver.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public static void setDefaultAudioVolume ()
{
    if (MainActivity.mainActivity == null) return;
    //
    float volumePercentage = OBConfigManager.sharedManager.getDefaultAudioVolumePercentage() / (float) 100;
    if (volumePercentage < 0) return;
    //
    AudioManager am = (AudioManager) MainActivity.mainActivity.getSystemService(Context.AUDIO_SERVICE);
    am.setStreamVolume(AudioManager.STREAM_MUSIC, Math.round(am.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * volumePercentage), 0);
}
 
Example 13
Source File: ChatActivity.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
private void muteMicBeep(boolean mute) {
	debug("muteMicBeep:" + mute + ":" + MainActivity.volume);
	AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);	
	if (mute) {
		//if its true then the Volume will be zero.
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
	} else {
		//if its false, the Volume will put back on
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, MainActivity.volume, 0);
	}
}
 
Example 14
Source File: ChatInputView.java    From aurora-imui with MIT License 5 votes vote down vote up
public void setAudioPlayByEarPhone(int state) {
    AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
    int currVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
    audioManager.setMode(AudioManager.MODE_IN_CALL);
    if (state == 0) {
        mIsEarPhoneOn = false;
        audioManager.setSpeakerphoneOn(true);
        audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
                audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), AudioManager.STREAM_VOICE_CALL);
    } else {
        mIsEarPhoneOn = true;
        audioManager.setSpeakerphoneOn(false);
        audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, currVolume, AudioManager.STREAM_VOICE_CALL);
    }
}
 
Example 15
Source File: ChatActivity.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
private void muteMicBeep(boolean mute) {
	debug("muteMicBeep:" + mute + ":" + MainActivity.volume);
	AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);	
	if (mute) {
		Log.d("ChatActivity","muteMicBeep : " + 0);
		//if its true then the Volume will be zero.
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
	} else {
		Log.d("ChatActivity","muteMicBeep : " + MainActivity.volume);
		//if its false, the Volume will put back on
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, MainActivity.volume, 0);
	}
}
 
Example 16
Source File: ChatActivity.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
private void muteMicBeep(boolean mute) {
	debug("muteMicBeep:" + mute + ":" + MainActivity.volume);
	AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);	
	if (mute) {
		Log.d("ChatActivity","muteMicBeep : " + 0);
		//if its true then the Volume will be zero.
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
	} else {
		Log.d("ChatActivity","muteMicBeep : " + MainActivity.volume);
		//if its false, the Volume will put back on
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, MainActivity.volume, 0);
	}
}
 
Example 17
Source File: PlayerService.java    From IdealMedia with Apache License 2.0 5 votes vote down vote up
public void volumeDown() {
    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);
    currVolume = currVolume - (maxVolume/10);
    if (currVolume < 0)
        currVolume = 0;

    am.setStreamVolume(AudioManager.STREAM_MUSIC, currVolume, AudioManager.FLAG_SHOW_UI);
}
 
Example 18
Source File: AlertPlayer.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
private void revertCurrentVolume(final Context ctx) {
    AudioManager manager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
    Log.i(TAG, "revertCurrentVolume volumeBeforeAlert " + volumeBeforeAlert + " volumeForThisAlert " + volumeForThisAlert
            + " currentVolume " + currentVolume);
    if (volumeForThisAlert == currentVolume && (volumeBeforeAlert != -1) && (volumeForThisAlert != -1)) {
        // If the user has changed the volume, don't change it again.
        manager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeBeforeAlert, 0);
    }
    volumeBeforeAlert = -1;
    volumeForThisAlert = - 1;
    
}
 
Example 19
Source File: MainActivity.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
@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 20
Source File: BtnVolume.java    From XposedNavigationBar with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void control(Context context, int value) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    //调整媒体声言,不播放声言也不振动
    am.setStreamVolume(AudioManager.STREAM_MUSIC, value, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}