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

The following examples show how to use android.media.AudioManager#getStreamMaxVolume() . 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: Notifications.java    From NightWatch with GNU General Public License v3.0 6 votes vote down vote up
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 2
Source File: VolumeControlAction.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private boolean shouldUpdateAudioVolume(boolean mute) {
    // Do nothing if in mute.
    if (mute) {
        return true;
    }

    // Update audio status if current volume position is edge of volume bar,
    // i.e max or min volume.
    AudioManager audioManager = tv().getService().getAudioManager();
    int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    if (mIsVolumeUp) {
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        return currentVolume == maxVolume;
    } else {
        return currentVolume == 0;
    }
}
 
Example 3
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 4
Source File: LingjuAudioPlayer.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
@Override
public void onAudioFocusChange(int focusChange) {
    Log.e(TAG, "audioFocusChangeListener.onAudioFocusChange>>>>>>>>>>>>>>>>>>" + focusChange);
    AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    switch (focusChange) {
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
            if (mPlayer != null) {
                volScalar = ((float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)) / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                // Log.i("LingJu", "获取媒体音量:" + volScalar);
                mPlayer.setVolume(0.2f, 0.2f);

            }
            break;
        }
        case AudioManager.AUDIOFOCUS_GAIN:
            if (mPlayer != null) {
                // Log.i("LingJu", "获取焦点后设置播放音量:" + volScalar);
                volScalar = volScalar == 0 ? 0.5f : volScalar;
                mPlayer.setVolume(volScalar, volScalar);
            }
            break;
        default:
            break;
    }
}
 
Example 5
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 6
Source File: Notifications.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static 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(bg_notification_sound);
    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 7
Source File: ServicePlayer.java    From freemp 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 8
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 9
Source File: OBSettingsContentObserver.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public String printVolumeStatus ()
{
    if (MainActivity.mainActivity == null) return "";
    //
    AudioManager am = (AudioManager) MainActivity.mainActivity.getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
    //
    return Math.round((currentVolume / (float) maxVolume) * 100.0f) + "%";
}
 
Example 10
Source File: OBSettingsContentObserver.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public boolean allowsLowerVolume ()
{
    if (MainActivity.mainActivity == null) return false;
    //
    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);
    return currentVolume > minVolumeLimit;
}
 
Example 11
Source File: SoundEngine.java    From homescreenarcade with GNU General Public License v3.0 5 votes vote down vote up
public void play(int sound) {
	AudioManager mgr = (AudioManager) context
			.getSystemService(Context.AUDIO_SERVICE);
	float streamVolumeCurrent = mgr
			.getStreamVolume(AudioManager.STREAM_RING);
	float streamVolumeMax = mgr
			.getStreamMaxVolume(AudioManager.STREAM_RING);
	float volume = streamVolumeCurrent / streamVolumeMax;

	sounds.play(soundsMap.get(sound), volume, volume, 1, 0, 1.f);
}
 
Example 12
Source File: ToggleRingPhone.java    From itracing2 with GNU General Public License v2.0 5 votes vote down vote up
private void startRing(Context context, Intent intent) {
    Log.d(TAG,"startRing()");
    if (currentRingtone != null) {
        currentRingtone.stop();
        currentRingtone = null;
    }
    final String address = intent.getStringExtra(Devices.ADDRESS);
    final String source = intent.getStringExtra(Devices.SOURCE);
    Uri sound = Uri.parse(Preferences.getRingtone(context, address, source));
    currentRingtone = RingtoneManager.getRingtone(context, sound);

    if (currentRingtone == null) {
        Toast.makeText(context, R.string.ring_tone_not_found, Toast.LENGTH_LONG).show();
        return;
    }

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    final int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
    audioManager.setStreamVolume(AudioManager.STREAM_RING, max, 0);

    currentRingtone.play();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final Notification notification = new Notification.Builder(context)
            .setContentText(context.getString(R.string.stop_ring))
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(false)
            .setOngoing(true)
            .setContentIntent(PendingIntent.getBroadcast(context, 0, new Intent(context, ToggleRingPhone.class), PendingIntent.FLAG_UPDATE_CURRENT))
            .build();
    notificationManager.notify(NOTIFICATION_ID, notification);
}
 
Example 13
Source File: AudioManagerUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定声音流最大音量大小
 * @param streamType 流类型
 * @return 最大音量大小
 */
public static int getStreamMaxVolume(final int streamType) {
    AudioManager audioManager = AppUtils.getAudioManager();
    if (audioManager != null) {
        try {
            return audioManager.getStreamMaxVolume(streamType);
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "getStreamMaxVolume");
        }
    }
    return 0;
}
 
Example 14
Source File: HostMediaPlayerManager.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 状態変化のイベントを通知.
 *
 * @param status ステータス
 */
public void sendOnStatusChangeEvent(final String status) {
    if (mOnStatusChangeEventFlag) {
        List<Event> events = EventManager.INSTANCE.getEventList(HostDevicePlugin.SERVICE_ID,
                MediaPlayerProfile.PROFILE_NAME, null, MediaPlayerProfile.ATTRIBUTE_ON_STATUS_CHANGE);

        AudioManager manager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);

        double maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        double mVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
        double mVolumeValue = mVolume / maxVolume;

        for (int i = 0; i < events.size(); i++) {

            Event event = events.get(i);
            Intent intent = EventManager.createEventMessage(event);

            MediaPlayerProfile.setAttribute(intent, MediaPlayerProfile.ATTRIBUTE_ON_STATUS_CHANGE);
            Bundle mediaPlayer = new Bundle();
            MediaPlayerProfile.setStatus(mediaPlayer, status);
            MediaPlayerProfile.setMediaId(mediaPlayer, mMyCurrentMediaId);
            MediaPlayerProfile.setMIMEType(mediaPlayer, mMyCurrentFileMIMEType);
            MediaPlayerProfile.setPos(mediaPlayer, mMyCurrentMediaPosition / UNIT_SEC);
            MediaPlayerProfile.setVolume(mediaPlayer, mVolumeValue);
            MediaPlayerProfile.setMediaPlayer(intent, mediaPlayer);
            sendEvent(intent, event.getAccessToken());
        }
    }
}
 
Example 15
Source File: TouchLayout.java    From DMusic with Apache License 2.0 5 votes vote down vote up
private void init(Context context) {
    mActivity = (Activity) context;
    touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    View root = LayoutInflater.from(context).inflate(R.layout.lib_player_layout_touch, this);
    initView(root);
    int[] size = Util.getScreenSize(mActivity);
    screenWidth = size[0];
    audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    gestureDetector = new GestureDetector(context, new PlayerGestureListener());

    root.setClickable(true);
    root.setOnTouchListener(this);
}
 
Example 16
Source File: CallViewActivity.java    From matrix-android-console with Apache License 2.0 5 votes vote down vote up
@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 17
Source File: GestureCover.java    From PlayerBase with Apache License 2.0 4 votes vote down vote up
private void initAudioManager(Context context) {
    audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    mMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}
 
Example 18
Source File: AudioUtils.java    From SimpleSmsRemote with MIT License 4 votes vote down vote up
private static int getVolumeIndexFromPercentage(AudioManager audioManager, int streamType, float volumePercentage) {
    int maxIndex = audioManager.getStreamMaxVolume(streamType);
    return Math.round(volumePercentage / 100f * (float) maxIndex);
}
 
Example 19
Source File: YouTubePlayerV2Fragment.java    From SkyTube with GNU General Public License v3.0 4 votes vote down vote up
@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 vote down vote up
/**
 * Get the maximum volume value of the media stream
 *
 * @param audioManager object
 * @return the integer value
 */
private static int getMaxMediaVolume(@NonNull final AudioManager audioManager) {
    return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}