简体   繁体   English

Android广播意图进行音频播放?

[英]Android broadcast intent for audio playback?

I'm trying to build an Android app that uses a broadcast receiver to detect when music playback starts, and then offer gesture control to trigger sending a skip track intent back out. 我正在尝试构建一个Android应用程序,该应用程序使用广播接收器来检测音乐播放何时开始,然后提供手势控制以触发发送跳过轨道意图。

To begin, I just want to set the receiver up to trigger a notification when audio playback starts so that I can check this concept will work. 首先,我只想将接收器设置为在音频播放开始时触发通知,以便我可以检查此概念是否有效。 I've declared a service and a receiver in my manifest file, and I've created a service class that (hopefully) creates a notification via onCreate(). 我在清单文件中声明了一个服务和一个接收器,并且我已经创建了一个服务类(希望通过onCreate()创建一个通知)。

I've crawled a tonne of documentation though and can't seem to find the correct intent to listen in for with my receiver. 我已经抓了一大堆文档但似乎无法找到正确的意图来接收我的接收器。 Surely something exists for this? 当然有什么东西存在吗?

Regards. 问候。

It turns out there's a new security feature in Android 3.1 and above, where broadcast receivers don't register until the user manually opens the app at least once. 事实证明Android 3.1及更高版本中有一个新的安全功能,广播接收器在用户至少手动打开应用程序之前不会注册。 Since I was trying to build a service-only app that had no UI to be opened, this was preventing all my intent listener attempts from working. 由于我试图构建一个只能打开UI的服务应用程序,这阻止了我的所有意图侦听器尝试工作。

I've amended to have a splash-screen activity and now everything is working as I'd hoped. 我已修改为有一个闪屏活动,现在一切都按照我希望的方式工作。 I'm now working to collate a list of player-specific intents to work from but for those trying to build something similar, com.android.music.playstatechanged seems to be the best start. 我现在正在努力整理一个特定于玩家的意图列表,但对于那些试图构建类似东西的人来说,com.android.music.playstatechanged似乎是最好的开始。 This one is triggered by the default player (Google Play Music) when music either starts, pauses, or resumes and bundles itself with extra data including a boolean 'playing' property to differentiate between the play and pause states. 当音乐开始,暂停或恢复时,默认播放器(Google Play音乐)触发此播放,并使用包含布尔“播放”属性的额外数据捆绑自身,以区分播放和暂停状态。

Essentially, this is the core receiver I'm now using. 从本质上讲,这是我现在使用的核心接收器。 Have fun! 玩得开心!

public class MainReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Start the service when music plays, stop the service when music ends.
        if(intent.hasExtra("playing")) {
            if(intent.getBooleanExtra("playing", false)) {
                Intent service = new Intent(context, MainService.class);
                context.startService(service);
            } else {
                Intent service = new Intent(context, MainService.class);
                context.stopService(service);
            }
        }
    }
}

Surely something exists for this? 当然有什么东西存在吗?

There are thousands of music players. 有成千上万的音乐播放器。 None are obligated to broadcast anything, let alone information about tracks. 没有人有义务播放任何内容,更不用说有关曲目的信息了。 And none are obligated to allow you to "skip track" via any means. 没有人有义务允许您通过任何方式“跳过轨道”。

You are welcome to search for specific music players that offer a documented and supported API for this sort of thing. 欢迎您搜索特定的音乐播放器,为这类事物提供文档化和支持的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM