简体   繁体   English

在Android中控制音乐播放器的音乐

[英]Control music of music player in Android

I want to program a music controller in my app, so I would be able to play/pause, skip to the next song or to the previous song in my playlist from another built-in music app. 我想在我的应用程序中对音乐控制器进行编程,这样我就可以播放/暂停,从另一个内置音乐应用程序跳到播放列表中的下一首歌曲或上一首歌曲。 I've seen a solution using broadcast to send messages to all music players to, for example, start playing music. 我已经看到了使用广播将消息发送到所有音乐播放器以开始播放音乐的解决方案。 The problem is, music starts playing on three different music players I have installed. 问题是,音乐开始在我安装的三个不同的音乐播放器上播放。 Does anybody know what to use instead of sendBroadcast , so that message will be sent only to the app I want? 有谁知道该用什么而不是sendBroadcast ,以便该消息仅发送到我想要的应用程序?

I found the following solution on the internet. 我在互联网上找到以下解决方案。 It uses the sendBroadcast method. 它使用sendBroadcast方法。 The message is broadcasted to every music player which I do not want. 该消息广播到我不需要的每个音乐播放器。 I want it to be sent only to one specific app. 我希望仅将其发送到一个特定的应用程序。

long eventTime = SystemClock.uptimeMillis();

/*NEXT*/
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent downEvent = new KeyEvent(eventTime, eventTime, 
KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
sendBroadcast(downIntent, null);

Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
KeyEvent upEvent = new KeyEvent(eventTime, eventTime, 
KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
sendBroadcast(upIntent, null);

You cannot send it to just one music player. 您不能将其仅发送到一个音乐播放器。 A broadcast is a system event that triggers an action that other apps can subscribe to. 广播是触发其他应用程序可以订阅的操作的系统事件。 Im pretty sure its very possible to write a music player that does not subscribe to those broadcasts unless Android forces that on you when playing back music (Im not sure wether or not it does, but I think not). 我非常确定,除非Android在播放音乐时强加您的声音,否则编写不订阅这些广播的音乐播放器很有可能(我不确定是否会这样做,但我认为不是)。 So, you are not even sending it to everybody, you send it to the system and whoever subscribed to it will then be able to act on it. 因此,您甚至没有将其发送给所有人,而是将其发送给系统,然后订阅该文件的任何人都可以对它采取行动。

What exactly is the usecase of this app? 这个应用程序的用例到底是什么?

Another way you can interact with other apps is an implicit Intent. 您可以与其他应用交互的另一种方式是隐式Intent。 With that you can call views from other apps and get a result back. 这样,您可以从其他应用程序调用视图并返回结果。 This only works with apps/views where the developer of that app allowed this. 这仅适用于该应用程序的开发人员允许的应用程序/视图。 And does not do what you want either. 而且也不做您想要的。

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

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