Android的音乐播放器应用程序:如何设置媒体播放器,在服务运行一个完整的听众? [英] Android music player app: how to set a complete listener for media player that is running in a service?

查看:96
本文介绍了Android的音乐播放器应用程序:如何设置媒体播放器,在服务运行一个完整的听众?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的音乐播放器应用程序。我有一个服务的MediaPlayer对象。问题是
我不知道如何更新从服务中的用户界面。举例我想从当前歌曲,但更新的剩余时间 - 因为MediaPlayer的是服务 - 我不能设置监听器MediaPlayer对象。

I'm writing an music player app. I have the MediaPlayer object in a service. The problem is that I don't know how to update the UI from service. By example I want to update the remaining time from current song but - because the MediaPlayer is on service - I can't set a listener to MediaPlayer object.

我在想发送广播消息,当前的时间,但如果我这样做,我必须在每秒发送消息。我想这会影响应用程序+电池寿命的速度。

I was thinking about sending a broadcast message with current time but if I'll do it in this way, I have to send a message on every second. I suppose this will affect the speed of application + battery life.

另一种解决方案可以是创建服务公共静态变量,然后从活动的访问,但我看,这是不是一个好的选择。

Another solution can be to create a public static variable in Service and access it from activity but I read that this is not a good option.

你觉得呢?你知道的任何 - 更好 - 的方式来更新从服务用户界面

What do you think? Do you know any - better - way to update the UI from service?

感谢您。

推荐答案

我还没有与媒体播放器的工作,但我假设你想从你的活动(使用startService)启动该服务开始播放音乐,然后要绑定到(使用bindService)的服务,以便能够注册监听器(服务将包装从媒体播放器的回调),将提供信息发回从服务到的活性和提供改变跟踪等的可能性而活动可见。

I haven’t worked with the media player, but I assume that you want to start the service from your activity (using startService) to start playing music, and then you want to bind to the service (using bindService) to be able to register listeners that will deliver information back from the service (the service will wrap the callbacks from the media player) to the activity and offer the possibility to change track etc. while the activity is visible.

所以,你需要实现你的服务既是启动服务(要能播放音乐,而你的应用程序在后台),它可以绑定到一个服务(要能时,控制它的应用是在前台)。假设你的服务在同一个进程中你的客户的活动是实现这个非常简单的运行。而且,我也建议你服务在前台运行,以避免它被系统杀死。

So, you need to implement your service as both a started service (to be able to play music while you application is in the background) and a service that it is possible to bind to (to be able to control it when your application is in the foreground). Assuming that you service is running in the same process as your client activity it is very simple to implement this. And, I would also recommend you service to be running in the foreground to avoid it being killed by the system.

启动的服务:的http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService

这是您的客户端活动应该绑定在显示时为本地服务:的http://developer.android.com/guide/topics/fundamentals/bound-services.html#Binder

Local service that your client activity should bind to when it is displayed: http://developer.android.com/guide/topics/fundamentals/bound-services.html#Binder

这篇关于Android的音乐播放器应用程序:如何设置媒体播放器,在服务运行一个完整的听众?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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