Android 音乐播放器应用程序:如何为服务中运行的媒体播放器设置完整的侦听器?

发布于 2025-01-03 14:32:24 字数 330 浏览 0 评论 0原文

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

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

另一个解决方案是在 Service 中创建一个 public static 变量并从 Activity 访问它,但我读到这不是一个好的选择。

你怎么认为?您知道从服务更新 UI 的任何更好的方法吗?

谢谢。

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?

Thank you.

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

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

发布评论

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

评论(2

不必在意 2025-01-10 14:32:24

您可以通过将服务绑定到 Activity 来完成此操作。在 Android 中使用 AIDL 执行此操作。

这是 AIDL 的一个很好的例子,

你真正需要做的是创建你的在您的 SERVICE 中的 MediaPlayer 中,创建一个扩展 ServiceConnection 的类,并在 .aidl 文件中创建一些接口方法,如下所示:

interface IPlayerService {
    // You can pass values in, out, or inout. 
    // Primitive datatypes (such as int, boolean, etc.) can only be passed in.

    void play();
     void pause();
     void stop();
     boolean isPlaying();
     int maxDuration();
     int getCurrentPosition();
     void changePosition(int a);

     void seekComplete();

     boolean isMusicCompleted();

} 

通过在服务的 onBind(Intent Intent) 方法中创建新的存根来在服务类中定义这些方法 班级。

现在,从您的 mainActivity 与服务交互以更新您的 UI 。

上面的 AIDL & 链接中的示例给出了如何执行这些操作; 将音乐应用绑定到服务

希望这将帮助您解决您的问题。

You can do this by binding your service to activity .Perform this using AIDL in android.

Here is a very good example of AIDL

What you actually need to do is create your MediaPlayer in your SERVICE, create a class that extends ServiceConnection and in your .aidl file make some interface methods like this :

interface IPlayerService {
    // You can pass values in, out, or inout. 
    // Primitive datatypes (such as int, boolean, etc.) can only be passed in.

    void play();
     void pause();
     void stop();
     boolean isPlaying();
     int maxDuration();
     int getCurrentPosition();
     void changePosition(int a);

     void seekComplete();

     boolean isMusicCompleted();

} 

Define these methods in your service class by creating a new stub in onBind(Intent intent) method of the service class.

Now from your mainActvity interact with service to update your UI .

How to perform these things is given by an example in the above link of AIDL & bind music app to service.

Hope this will help you to solve your issue.

久夏青 2025-01-10 14:32:24

我没有使用过媒体播放器,但我假设您希望从 Activity 启动服务(使用 startService)来开始播放音乐,然后您希望绑定到服务(使用 bindService)以便能够注册侦听器将从服务(服务将包装来自媒体播放器的回调)传递信息回活动,并提供在活动可见时更改轨道等的可能性。

因此,您需要将您的服务实现为启动服务(以便能够在应用程序处于后台时播放音乐)和可以绑定到的服务(以便能够在应用程序处于后台时控制它)。前景)。假设您的服务与您的客户端活动在同一进程中运行,那么实现这一点非常简单。而且,我还建议您的服务在前台运行,以避免它被系统杀死。

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

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

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.

Started service: http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService

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

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