在 Android 上收听音量变化事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11318933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:56:18  来源:igfitidea点击:

Listen to volume changes events on Android

androidevent-handlingvolume

提问by android developer

Is there any way to listen to the event of volume change on Android, without just taking over the volume buttons?

有没有什么办法可以在Android上监听音量变化的事件,而不只是接管音量按钮?

The only thing I've found that works is here, but it works only after the volume control has disappeared.

我发现唯一有效的是here,但它只有在音量控制消失后才有效。

Not all devices have volume buttons, and I need to capture the volume changes as soon as they occur, and not after the volume dialog is gone.

并非所有设备都有音量按钮,我需要在音量变化发生后立即捕捉,而不是在音量对话框消失后。

回答by Tad

Better, you can register a ContentObserveras follows:

更好的是,您可以ContentObserver按如下方式注册:

  getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, new ContentObserver(){...} );

Your ContentObserver might look like this:

您的 ContentObserver 可能如下所示:

public class SettingsContentObserver extends ContentObserver {
    private AudioManager audioManager;

    public SettingsContentObserver(Context context, Handler handler) {
        super(handler);
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return false;
    }

    @Override
    public void onChange(boolean selfChange) {
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        Log.d(TAG, "Volume now " + currentVolume);
    }
}

When done:

完成后:

getApplicationContext().getContentResolver().unregisterContentObserver(mContentObserver);

One caution, though - sometimes the notifications seem to be delayed if there are lots of button presses quickly.

但是,请注意 - 如果快速按下大量按钮,有时通知似乎会延迟。

回答by android developer

ok , for now , what i do is to listen to the volume buttons using onKeyDown (and check for KEYCODE_VOLUME_DOWN,KEYCODE_VOLUME_MUTE,KEYCODE_VOLUME_UP ) , and using a handler i've posted a new runnable that checks the volume level .

好的,现在,我所做的是使用 onKeyDown 收听音量按钮(并检查 KEYCODE_VOLUME_DOWN、KEYCODE_VOLUME_MUTE、KEYCODE_VOLUME_UP),并使用我发布的处理程序来检查音量级别。

also , since some devices have a volume dialog , i've added a listener to when it is being disappeared , according to this link.

此外,由于某些设备具有音量对话框,因此根据此链接,我在它消失时添加了一个侦听器。

回答by Apirak Lunla

Use broadcast receiver VOLUME_CHANGED_ACTION then use AudioManager to obtain current volume.

使用广播接收器 VOLUME_CHANGED_ACTION 然后使用 AudioManager 获取当前音量。

<receiver android:name="VolumeChangeReceiver" >
    <intent-filter>
         <action android:name="android.media.VOLUME_CHANGED_ACTION" />
    </intent-filter>
</receiver>

回答by Mouna Cheikhna

You can use : registerMediaButtonEventReceiver(ComponentName eventReceiver) which registers a component to be the sole receiver of MEDIA_BUTTON intents.

您可以使用: registerMediaButtonEventReceiver(ComponentName eventReceiver),它将组件注册为 MEDIA_BUTTON 意图的唯一接收器。

//  in your activity.
MediaButtonReceiver receiver = new MediaButtonReceiver();

// in onCreate put
registerMediaButtonEventReceiver(receiver); 

class MediaButtonReceiver implements BroadcastReceiver {
     void onReceive(Intent intent) {
          KeyEvent ke = (KeyEvent)intent.getExtra(Intent.EXTRA_KEY_EVENT); 
          if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
            //action when volume goes down
          }
           if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
              //action when volume goes up
          }
     } 
}

   //In both onStop and onPause put :
   unregisterMediaButtonEventReceiver(receiver);

what we are doing here is defining a BroadcastReceiver that deals with ACTION_MEDIA_BUTTON. and use EXTRA_KEY_EVENT which is containing the key event that caused the broadcast to get what was pressed and act upon that.

我们在这里所做的是定义一个处理 ACTION_MEDIA_BUTTON 的 BroadcastReceiver。并使用 EXTRA_KEY_EVENT ,它包含导致广播获得按下的内容并对其采取行动的关键事件。