即使手机处于静音/请勿打扰模式,也可以播放声音

时间:2018-11-14 06:44:34

标签: android

我有一个用例,即使手机处于API级别> 23的静音模式下,我也要播放声音。经过一番谷歌搜索后,我发现我应该明确地征询用户的许可。现在,使用以下代码设置所有权限。

private void requestForDoNotDisturbPermissionOrSetDoNotDisturbForApi23AndUp() {
        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        // Open Setting screen to ask for permisssion
        Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
        startActivityForResult( intent, ON_DO_NOT_DISTURB_CALLBACK_CODE );
        startActivity(intent);
    }

调整音量后,​​还可以使用媒体播放器以静音模式播放声音。

mAudioManager.setStreamVolume(stream, deviceLocalVolume,
            AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

播放后,声音将重设为原始音量。但是我看到电话不再处于请勿打扰模式。为什么这样?

编辑:详细代码:

void playSound(int stream) {
    mAudioManager = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);

    int deviceLocalVolume = getDeviceVolume(volume,
            mAudioManager.getStreamMaxVolume(stream));

    Log.d(TAG,
            "device max volume =  "
                    + mAudioManager.getStreamMaxVolume(stream)
                    + " for streamType " + stream);
    Log.d(TAG, "playing sound " + uri.toString()
            + " with device local volume " + deviceLocalVolume);

    oldVolume = mAudioManager.getStreamVolume(stream);
    Log.d(Constants.APP_TAG, "setting device local volume to "
            + deviceLocalVolume);
    mAudioManager.setStreamVolume(stream, deviceLocalVolume,
            AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

    final MediaPlayer mediaPlayer = new MediaPlayer();
    golbalMMediaPlayer = mediaPlayer;

    try {
        final OnPreparedListener OnPreparedListener = new OnPreparedListener() {

            @Override
            public void onPrepared(final MediaPlayer mp) {
                Log.d(TAG, "OnPreparedListener prepared.Actually playing music now");
                mp.start();
                countDownTimer = new CountDownTimer(maxTime * 1000,
                        tickTime * 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        Log.d(TAG, "tick while playing sound ");
                    }

                    @Override
                    public void onFinish() {
                        Log.d(TAG, "timer finished");
                        resetVolume();
                        stopPlaying();
                    }
                };
                countDownTimer.start();
            }
        };

        mediaPlayer.setDataSource(context.getApplicationContext(), uri);
        mediaPlayer.setAudioStreamType(stream);
        mediaPlayer.setLooping(false);
        mediaPlayer.setOnPreparedListener(OnPreparedListener);
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d(TAG, "Done playing music now");
                resetVolume();
                Log.d(TAG, "releasing mediaplayer now");
                try {
                    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                        mediaPlayer.release();
                    }
                } catch (Exception ex) {
                    Log.e(Constants.APP_TAG,
                            "error on oncompletion listener", ex);
                }
            }
        });
        mediaPlayer.prepareAsync();
}

private static int getDeviceVolume(int currentVolume, int deviceMaxVolume) {
    return (int) Math.ceil((deviceMaxVolume * currentVolume) / 100);
}

private static void resetVolume() {
    try {
        Log.d(Constants.APP_TAG, "reseting volume to " + oldVolume
                + " for stream = " + stream);
        mAudioManager.setStreamVolume(stream, oldVolume,
                AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
    } catch (Exception ex) {
        Log.e("error", "error", ex);
    }
}

1 个答案:

答案 0 :(得分:0)

您必须重新打开DnD:

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="?android:attr/actionBarSize"

请参见INTERRUPTION_FILTER_NONE

  

除符合优先级条件的那些通知外,所有通知均被抑制。

     

某些音频流被静音。