android - 是否可以对 Android 应用程序中的耳机音量增大/减小键使用react?

标签 android media

我想知道是否真的可以对 Android 应用程序中的耳机音量控制按钮使用react。每当我使用支持 iPhone/iPod 的耳机时,我可以在各种音乐应用程序中使用暂停/快进/快退按钮,但没有一个对音量控制有反应。

我搜索了很多,但没有找到任何可以直接解决这个问题的东西。

我很好奇这是否是 Android 平台或我正在使用的应用程序的限制。如果不是限制,我将如何在我自己的 Android 应用程序中从耳机捕获音量摇杆事件。

iPhone/iPod 似乎能够通过 3.5 毫米插孔对所有耳机事件使用react。在我看来,它在 Android 中也应该是可能的。

最佳答案

这 100% 是与应用程序本身有关的问题。有很多很好的例子可以解决这个问题。例如,您可以通过以下方式解决此问题:

private MPR mediaPlayerReceiver = new MPR();

onCreate() {
   ...
   IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
   // this is nasty, since most apps set it to 9999, so you need to be higher
   mediaFilter.setPriority(999);
   this.registerReceiver(mediaPlayerReceiver, mediaFilter);
   ...
}

MPR  extends BroadcastReceiver {
    public MPR() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            return;
        }
        KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }
        try {
            int action = event.getAction();
            switch(action) {
                case KeyEvent.ACTION_DOWN :
                    // handle play/pause here
                    break;
            }
        } catch (Exception e) {
            // something bad happened
        }
        abortBroadcast();
    }
}

关于android - 是否可以对 Android 应用程序中的耳机音量增大/减小键使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5584605/

相关文章:

javascript - 在 Android WebView 中隐藏 HTML 元素?

java - 如何以编程方式在 ListActivity 上查找 TextView

android - 致命异常 asyncTask #1 主机可能不为空

php - 将android应用程序链接到mysql数据库手机并链接信息

java - 如何在改造中获取访问 token

javascript - 有没有办法通过javascript找出Chrome的媒体设置环境?

xamarin.forms - Xamarin 表单 : Camera and gallery are not working in android, 显示权限异常

ios - Matt Gallagher的AudioStreamer从播放状态前的偏移量播放mp3

java - 使用 JMF 将多路复用音频/视频录制到文件

image - Sitecore:向低带宽客户端提供较低分辨率的图像