Java Code Examples for android.media.AudioManager#FLAG_PLAY_SOUND

The following examples show how to use android.media.AudioManager#FLAG_PLAY_SOUND . 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: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void dispatchVolumeKeyEventLocked(String packageName, int pid, int uid,
        boolean asSystemService, KeyEvent keyEvent, int stream, boolean musicOnly) {
    boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
    boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
    int direction = 0;
    boolean isMute = false;
    switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            direction = AudioManager.ADJUST_RAISE;
            break;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            direction = AudioManager.ADJUST_LOWER;
            break;
        case KeyEvent.KEYCODE_VOLUME_MUTE:
            isMute = true;
            break;
    }
    if (down || up) {
        int flags = AudioManager.FLAG_FROM_KEY;
        if (musicOnly) {
            // This flag is used when the screen is off to only affect active media.
            flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
        } else {
            // These flags are consistent with the home screen
            if (up) {
                flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
            } else {
                flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE;
            }
        }
        if (direction != 0) {
            // If this is action up we want to send a beep for non-music events
            if (up) {
                direction = 0;
            }
            dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
                    direction, flags);
        } else if (isMute) {
            if (down && keyEvent.getRepeatCount() == 0) {
                dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
                        AudioManager.ADJUST_TOGGLE_MUTE, flags);
            }
        }
    }
}
 
Example 2
Source File: MediaSessionRecord.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Send a volume adjustment to the session owner. Direction must be one of
 * {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE},
 * {@link AudioManager#ADJUST_SAME}.
 *
 * @param packageName The package that made the original volume request.
 * @param pid The pid that made the original volume request.
 * @param uid The uid that made the original volume request.
 * @param caller caller binder. can be {@code null} if it's from the volume key.
 * @param asSystemService {@code true} if the event sent to the session as if it was come from
 *          the system service instead of the app process. This helps sessions to distinguish
 *          between the key injection by the app and key events from the hardware devices.
 *          Should be used only when the volume key events aren't handled by foreground
 *          activity. {@code false} otherwise to tell session about the real caller.
 * @param direction The direction to adjust volume in.
 * @param flags Any of the flags from {@link AudioManager}.
 * @param useSuggested True to use adjustSuggestedStreamVolume instead of
 */
public void adjustVolume(String packageName, int pid, int uid,
        ISessionControllerCallback caller, boolean asSystemService, int direction, int flags,
        boolean useSuggested) {
    int previousFlagPlaySound = flags & AudioManager.FLAG_PLAY_SOUND;
    if (isPlaybackActive() || hasFlag(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY)) {
        flags &= ~AudioManager.FLAG_PLAY_SOUND;
    }
    if (mVolumeType == PlaybackInfo.PLAYBACK_TYPE_LOCAL) {
        // Adjust the volume with a handler not to be blocked by other system service.
        int stream = AudioAttributes.toLegacyStreamType(mAudioAttrs);
        postAdjustLocalVolume(stream, direction, flags, packageName, uid, useSuggested,
                previousFlagPlaySound);
    } else {
        if (mVolumeControlType == VolumeProvider.VOLUME_CONTROL_FIXED) {
            // Nothing to do, the volume cannot be changed
            return;
        }
        if (direction == AudioManager.ADJUST_TOGGLE_MUTE
                || direction == AudioManager.ADJUST_MUTE
                || direction == AudioManager.ADJUST_UNMUTE) {
            Log.w(TAG, "Muting remote playback is not supported");
            return;
        }
        mSessionCb.adjustVolume(packageName, pid, uid, caller, asSystemService, direction);

        int volumeBefore = (mOptimisticVolume < 0 ? mCurrentVolume : mOptimisticVolume);
        mOptimisticVolume = volumeBefore + direction;
        mOptimisticVolume = Math.max(0, Math.min(mOptimisticVolume, mMaxVolume));
        mHandler.removeCallbacks(mClearOptimisticVolumeRunnable);
        mHandler.postDelayed(mClearOptimisticVolumeRunnable, OPTIMISTIC_VOLUME_TIMEOUT);
        if (volumeBefore != mOptimisticVolume) {
            pushVolumeUpdate();
        }
        mService.notifyRemoteVolumeChanged(flags, this);

        if (DEBUG) {
            Log.d(TAG, "Adjusted optimistic volume to " + mOptimisticVolume + " max is "
                    + mMaxVolume);
        }
    }
}
 
Example 3
Source File: VolumePanel.java    From Noyze with Apache License 2.0 4 votes vote down vote up
protected int getFlags(int streamType) {
	// NOTE: NEVER use AudioManager.FLAG_SHOW_UI, this will display
    // the actual Android VolumePanel and ruin the ambiance.
    if (streamType == USE_DEFAULT_STREAM_TYPE) streamType = lastStreamType;
    int flags = 0;
    boolean streamAffectedByRinger;

    try {
        // Reported in Google Play developer console.
        streamAffectedByRinger = mAudioHelper.isStreamAffectedByRingerMode(streamType);
    } catch (Throwable t) {
        LOGE("VolumePanel", "Error determining ringer mode flag.", t);
        streamAffectedByRinger = false;
    }

    LOGI("VolumePanel", "getFlags(" + VolumeManager.getStreamName(streamType) +
            "), change=" + ringerMode + ", mRingerMode=" +
            mRingerMode + ", ringerAffected=" + streamAffectedByRinger);

	/*switch (ringerMode) {
        case Constants.RINGER_MODE_SILENT:
            if (streamAffectedByRinger)
                return AudioManager.FLAG_ALLOW_RINGER_MODES;
            return flags;

    }*/

    // For the default, let's check the system settings.
    switch (mRingerMode) {
        case RINGER_MODE_NORMAL:
            flags =  AudioManager.FLAG_PLAY_SOUND            |
                     AudioManager.FLAG_VIBRATE;
            break;
        case RINGER_MODE_VIBRATE:
            flags = AudioManager.FLAG_VIBRATE;
            break;
    }

    // Special mode, ALWAYS play a sound!
    if (ringerMode == Constants.RINGER_MODE_RING) {
        if ((flags & AudioManager.FLAG_PLAY_SOUND) != AudioManager.FLAG_PLAY_SOUND) {
            flags |= AudioManager.FLAG_PLAY_SOUND;
        }
    } else if (ringerMode == Constants.RINGER_MODE_SILENT) {
        if ((flags & AudioManager.FLAG_PLAY_SOUND) == AudioManager.FLAG_PLAY_SOUND) {
            flags &= ~AudioManager.FLAG_PLAY_SOUND;
        }
    }

    // Handle the addition of the ringer mode based on the stream type.
    /*if (streamAffectedByRinger)
        if (flags == 0)
            flags = AudioManager.FLAG_ALLOW_RINGER_MODES;
        else
            flags |= AudioManager.FLAG_ALLOW_RINGER_MODES;*/

	return flags;
}
 
Example 4
Source File: VolumePanel.java    From Noyze with Apache License 2.0 4 votes vote down vote up
protected int getFlags(int streamType) {
	// NOTE: NEVER use AudioManager.FLAG_SHOW_UI, this will display
    // the actual Android VolumePanel and ruin the ambiance.
    if (streamType == USE_DEFAULT_STREAM_TYPE) streamType = lastStreamType;
    int flags = 0;
    boolean streamAffectedByRinger;

    try {
        // Reported in Google Play developer console.
        streamAffectedByRinger = mAudioHelper.isStreamAffectedByRingerMode(streamType);
    } catch (Throwable t) {
        LOGE("VolumePanel", "Error determining ringer mode flag.", t);
        streamAffectedByRinger = false;
    }

    LOGI("VolumePanel", "getFlags(" + VolumeManager.getStreamName(streamType) +
            "), change=" + ringerMode + ", mRingerMode=" +
            mRingerMode + ", ringerAffected=" + streamAffectedByRinger);

	/*switch (ringerMode) {
        case Constants.RINGER_MODE_SILENT:
            if (streamAffectedByRinger)
                return AudioManager.FLAG_ALLOW_RINGER_MODES;
            return flags;

    }*/

    // For the default, let's check the system settings.
    switch (mRingerMode) {
        case RINGER_MODE_NORMAL:
            flags =  AudioManager.FLAG_PLAY_SOUND            |
                     AudioManager.FLAG_VIBRATE;
            break;
        case RINGER_MODE_VIBRATE:
            flags = AudioManager.FLAG_VIBRATE;
            break;
    }

    // Special mode, ALWAYS play a sound!
    if (ringerMode == Constants.RINGER_MODE_RING) {
        if ((flags & AudioManager.FLAG_PLAY_SOUND) != AudioManager.FLAG_PLAY_SOUND) {
            flags |= AudioManager.FLAG_PLAY_SOUND;
        }
    } else if (ringerMode == Constants.RINGER_MODE_SILENT) {
        if ((flags & AudioManager.FLAG_PLAY_SOUND) == AudioManager.FLAG_PLAY_SOUND) {
            flags &= ~AudioManager.FLAG_PLAY_SOUND;
        }
    }

    // Handle the addition of the ringer mode based on the stream type.
    /*if (streamAffectedByRinger)
        if (flags == 0)
            flags = AudioManager.FLAG_ALLOW_RINGER_MODES;
        else
            flags |= AudioManager.FLAG_ALLOW_RINGER_MODES;*/

	return flags;
}