控制Android的媒体播放器 [英] Control Android Media Player

查看:223
本文介绍了控制Android的媒体播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来控制从我自己的服务的默认Android的媒体播放器。我的不可以兴趣玩从我的服务或活动的媒体;我想控制现有的应用程序,它由一个活动的( MediaPlaybackActivity.java ),更重要的是服务(<$ C C $> MediaPlaybackService.java )>封装/应用/音乐/ src目录/ COM /安卓/音乐/ 。理想情况下,我想一个解决方案,它是独立于Android的版本。

我已经找到了如何使用意图的操作做暂停/播放/停止/下一个/ previous。我可以拦截通过广播接收器的轨道变化事件。我还可以得到播放列表的列表和每个播放列表中的内容。 我希望能够做的是指示 MediaPlaybackService 来播放特定的文件/歌曲。同样,我不想打这首歌曲在我的应用程序;我想在Android默认媒体播放器进行播放。

我已经尝试了两种方法至今:

  1. 我进口从封装/应用/音乐/ src目录/ COM /安卓/音乐IMediaPlaybackService.aidl文件到自己的应用程序,并用它来绑定到 MediaPlaybackService 。在升级Froyo,这个伟大的工程;我可以通过一个路径中openFile方法,并且该服务将播放该文件。然而,姜饼,我得到一个错误:权限拒绝:访问服务ComponentInfo {com.google.android.music / com.android.music.MediaPlaybackService}从 PID = 17721,UID = 10058需要空。找到一个解决方法,这样的错误将是一个很好的临时解决方案,但它不是面向未来的。该服务很可能在Android中的未来版本中再次更改。

  2. 通过ACTION_VIEW意图启动媒体播放器。这也适用;然而,正如所料,该媒体播放器的用户界面被带到前面,这是不理想的。

      

    意向意图=新的意图(android.content.Intent.ACTION_VIEW);   intent.setDataAndType(someUri,音频/ MP3);   startActivity(意向);

有没有做到这一点任何其他方式?是否有意向我错过了会指示媒体播放器播放特定歌曲?或者,有没有去启动一个活动的背景下,或启动活动,并立即切换回previous一个?

解决方案

对不起,没有支持的方式做到这一点。所有你所描述的东西是使用私有的实现细节,正如你所看到的是不稳健。如果你想播放音乐,你应该这样做在自己的应用程序。

I'm looking for a way to control the default Android media player from my own service. I'm NOT interested in playing media from my service or activity; I want to control the existing application, which consists of an activity (MediaPlaybackActivity.java) and more importantly a service (MediaPlaybackService.java) located in packages/apps/Music/src/com/android/music/. Ideally, I would like a solution that is independent of the version of Android.

I have figured out how to do pause/play/stop/next/previous operations using Intents. I can intercept track change events using a broadcast receiver. I can also get a list of playlists and the contents of each playlist. What I would like to be able to do is instruct the MediaPlaybackService to play a specific file/song. Again, I don't want to play this song in my application; I want the Android default media player to play it.

I have tried two approaches so far:

  1. I imported the IMediaPlaybackService.aidl file from packages/apps/Music/src/com/android/music into my own application, and used this to bind to the MediaPlaybackService. On Froyo, this works great; I can pass a path to the openFile method, and the service will play the file. However, on Gingerbread, I get an error: Permission Denial: Accessing service ComponentInfo{com.google.android.music/com.android.music.MediaPlaybackService} from pid=17721, uid=10058 requires null. Finding a workaround to this error would be a good temporary solution, but it's not future-proof. This service may well change again in future versions of Android.

  2. Starting the media player via an ACTION_VIEW Intent. This also works; however, as expected, the media player UI is brought to the front, which is not ideal.

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW); intent.setDataAndType(someUri, "audio/mp3"); startActivity(intent);

Is there any other way to accomplish this? Is there an Intent I missed that would instruct the media player to play a specific song? Or alternatively, is there away to start an activity in the background, or to start an activity and immediately switch back to the previous one?

解决方案

Sorry, there is no supported way to do this. All of the stuff you are describing is using private implementation details, and as you have seen is not robust. If you want to play music, you should do it in your own app.

这篇关于控制Android的媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆