不调用onAdjustVolume

编程入门 行业动态 更新时间:2024-10-23 13:32:33
本文介绍了不调用onAdjustVolume的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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

本文发布于:2023-10-28 19:20:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537547.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:onAdjustVolume

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!