可以从 Android 应用程序跳过曲目吗?

发布于 2024-08-29 02:41:53 字数 243 浏览 5 评论 0原文

我正计划为 Android 2.1 开发一个应用程序,该应用程序每分钟都会更改歌曲(通过我希望 Android 中存在的“下一个”),以便使用音频设备 atm 的应用程序。

因此,如果我已经在后台运行 Spotify 播放音乐,我可以通过我的程序切换到下一首曲目吗?

如果我有任何不清楚的地方请告诉我。 提前致谢!

I'm planning on doing a application for Android 2.1 that changes song every minute (through what I hope exists in Android, "next") for the application using the audio device atm.

So if I have Spotify running in background already, playing music, can I through my program change to the next track?

Let me know if I was unclear about anything.
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一江春梦 2024-09-05 02:41:54

我知道这是一个有点老的问题,但我花了一些时间搜索除此处提到的内容之外的其他内容。

有一个解决方法 - 广播媒体按钮操作。有一个问题 - 接收者可以识别广播是来自系统还是来自另一个应用程序,因此他们可以忽略非系统广播。

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
synchronized (this) {
            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
            sendOrderedBroadcast(i, null);

            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
            sendOrderedBroadcast(i, null);
 }

I know this is a bit old question, but it took me some time searching something other then what is mentioned here.

There is a workaround - broadcasting media button action. There is one catch - receiver can recognize if the broadcast was from system or from another app, so they can ignore the non-system broadcasts.

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
synchronized (this) {
            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
            sendOrderedBroadcast(i, null);

            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
            sendOrderedBroadcast(i, null);
 }
但可醉心 2024-09-05 02:41:54

音乐应用程序没有通用的音频传输 API,因此您需要查看您所定位的音乐应用程序是否公开公开服务绑定或意图。如果没有,您将无法执行此操作。

There's no universal audio transport API for music applications, so you'd need to see if the music applications you're targeting publicly expose service bindings or intents. If not, you won't be able to do this.

北斗星光 2024-09-05 02:41:54

刚刚发布了相关答案此处

使用AudioManager的dispatchMediaKeyEvent()方法和定义的KeyEvent对我有用最新的SDK。

Just posted a relevant answer here

Using the AudioManager's dispatchMediaKeyEvent() method with a defined KeyEvent worked for me using the latest SDK.

反目相谮 2024-09-05 02:41:54

系统音乐主屏幕小部件为内置音乐播放器发送此意图:

    final ComponentName serviceName = new ComponentName(context, 
        MediaPlaybackService.class);
    intent = new Intent(MediaPlaybackService.NEXT_ACTION);
    intent.setComponent(serviceName);
    pendingIntent = PendingIntent.getService(context,
            0 /* no requestCode */, intent, 0 /* no flags */);
    views.setOnClickPendingIntent(R.id.control_next, pendingIntent);

但看起来这可能需要一些黑客才能在音乐应用程序本身中实现外部包,因为 MediaPlaybackService 只接受显式意图并且无法从外部访问。 此帖子似乎表明有可能不过,黑客行为。

但即便如此,正如罗曼所说,并不是每个音乐播放器都会尊重这个意图。您必须与 Spotify/Pandora/Last.fm 本身核实,看看他们是否有任何可用的意图来进行这样的绑定。

The system music homescreen widget sends this intent for the built-in music player:

    final ComponentName serviceName = new ComponentName(context, 
        MediaPlaybackService.class);
    intent = new Intent(MediaPlaybackService.NEXT_ACTION);
    intent.setComponent(serviceName);
    pendingIntent = PendingIntent.getService(context,
            0 /* no requestCode */, intent, 0 /* no flags */);
    views.setOnClickPendingIntent(R.id.control_next, pendingIntent);

But it looks like this might take some hackery to implement outside packages in the music app itself because the MediaPlaybackService only accepts explicit Intents and isn't accessible from the outside. This thread seems to indicate it's possible with a bit of hackery, though.

But even then, as Roman said, not every music player will respect that Intent. You'll have to check with Spotify/Pandora/Last.fm themselves and see if they have any available intents to bind like that.

花海 2024-09-05 02:41:54

看起来可以使用 AudioManager 来注入媒体密钥。

这是 另一个问题

this.mAudioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);

long eventtime = SystemClock.uptimeMillis();

KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(downEvent);

KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(upEvent);

同样的方式你可以注入PlayPause按钮和其他一些按钮。
我已经在控制 Youtube 的后台服务中对其进行了测试,它适用于 Android 6

Looks that it's possible to use AudioManager to inject media keys.

Here is a snippet from another question

this.mAudioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);

long eventtime = SystemClock.uptimeMillis();

KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(downEvent);

KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
mAudioManager.dispatchMediaKeyEvent(upEvent);

The same way you can inject PlayPause button and some others.
I've tested it within a background service controlling Youtube and it worked for Android 6

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文