我正在尝试使用Android 5.0以上版本的MediaSession在服务中获取HARDWARE_HOOK按钮和音量更改按钮.当屏幕关闭/锁定时,这必须特别起作用.问题是,尽管我能够接收HARDWARE_HOOK按钮事件,但未检测到音量变化.onAdjustVolume方法永远不会被调用.
I am trying to get HARDWARE_HOOK button and volume change button in my service using MediaSession for Android 5.0+ . This has to work specially when screen is off/locked. The problem is that though I am able to receive HARDWARE_HOOK button event, the volume change is not detected. the onAdjustVolume method never gets called.
import android.app.Service; import android.content.Intent; import android.media.MediaRouter; import android.media.session.MediaSession; import android.os.Binder; import android.os.IBinder; import android.support.v4.media.VolumeProviderCompat; import android.support.v4.media.session.MediaSessionCompat; import android.util.Log; import android.view.KeyEvent; import android.widget.Toast; public class HookButtonService extends Service{ public static final String SESSION_TAG = "SampleApp"; private MediaSessionCompat mMediaSession; private VolumeProviderCompat myVolumeProvider; public class ServiceBinder extends Binder { public HookButtonService getService() { return HookButtonService.this; } } private Binder mBinder = new ServiceBinder(); private MediaSessionCompat.Callback mMediaSessionCallback = new MediaSessionCompat.Callback() { @Override public boolean onMediaButtonEvent(Intent mediaButtonEvent) { Log.d("SampleApp","Media button received"); if(!MyApplication.isActivityVisible()) { Intent dialogIntent = new Intent(getApplicationContext(), MainActivity.class); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(dialogIntent); } return true; } }; public HookButtonService() { } public MediaSessionCompat.Token getMediaSessionToken() { return mMediaSession.getSessionToken(); } @Override public IBinder onBind(Intent intent) { return mBinder; } @Override public void onCreate() { super.onCreate(); mMediaSession = new MediaSessionCompat(this, SESSION_TAG); mMediaSession.setCallback(mMediaSessionCallback); myVolumeProvider = new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE, 100, 50) { @Override public void onAdjustVolume(int direction) { Log.d("SampleApp","Volume change received: "+direction); } }; mMediaSession.setActive(true); mMediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); mMediaSession.setPlaybackToRemote(myVolumeProvider); } @Override public void onDestroy() { super.onDestroy(); mMediaSession.release(); } }当您使用播放某些音乐/视频时,需要
推荐答案
onAdjustVolume 和 VolumeProviderCompat 与远程显示器配合使用铸件.例如,当您使用设备上的按钮时,该方法将不会触发沉默和遮蔽.令人困惑的类名,听起来不那么普遍...
onAdjustVolume and VolumeProviderCompat are desired to work with remote displays, when you play some music/video using casting. method won't fire when you are using on-device buttons during e.g. silence and screen-off. confusing class name, its not so universal as it sounds...
afaik在2021年初没有可靠的方法来在屏幕关闭时拦截音量按钮的按下,甚至可能检测到...(@ op,如果您找到解决方案请发表)
afaik at the beginning of 2021 there is no reliable way for intercepting volume button presses when screen off, probably even detecting... (@op if you found solution please post)
原因可能是这是预期的功能"之一.用于减少屏幕关闭过程中的电池消耗的Android版本-直接按下按钮可降低音频管理层,从而省略了框架/应用程序,不允许执行一些其他代码(我们要这样做)
The reason is probably that this is one of intended "features" of Android for reducing battery draining during screen off - distributing button presses straight to lower audio-managing layers ommiting framework/apps, not allowing to execute some additional code (which we want to do)
更多推荐
不调用onAdjustVolume
发布评论