在所有活动中显示一个共同的音乐播放器 [英] Showing a common music player in all activities

本文介绍了在所有活动中显示一个共同的音乐播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为乐队开发一款应用,其中包含多项不同的活动,例如议程、新闻部分、专辑部分等.因为是乐队,所以我想在后台播放乐队的歌曲.

I'm working on an app for a band, which consists of several different activities such as an agenda, news section, album section et cetera. Since it's for a band, I would like to play the band's songs in the background.

这不是问题,但我想在所有活动的底部显示一个小型音乐播放器,以便用户可以随时停止、跳过或重播歌曲.例如,可以在 Mobile Roadie 的应用程序中找到这样的示例.

This isn't such a problem, but I would like to show a small music player on the bottom of all activities so users can always stop, skip, or replay a song if they want to. An example of this can be found in the apps of Mobile Roadie, for instance.

但是我不知道如何做到这一点.我最好的猜测是创建一个具有自己布局的音乐播放器类,并将其包含在每个活动/布局的底部.但由于每个活动都在自己的线程中开始,我猜这会为每个活动重置播放器.

However I have no idea how to do this. My best guess is creating a music player class with its own layout, and including this at the bottom of each activity/layout. But since every activity starts in its own thread, I'm guessing this would reset the player for each activity.

那么,我将如何将一个通用的音乐播放器添加到我的所有活动中,音乐播放器的状态将在其中保持?

So, how would I go about adding a common music player to all of my activities, where the state of the music player would be maintained?

例如,下载 Mobile Roadie 的任何基于音乐的应用程序,您就会明白我的意思.

For an example, download any of the music based apps of Mobile Roadie and you'll know what I mean.

我决定使用 Fragments,但我不确定是否可以在现有活动中使用它们.我发现的所有示例都专注于全新的项目,而不是现有的项目.简而言之,您可以创建片段并在现有活动中使用它们吗?(例如在现有议程活动的顶部显示音乐播放器)

I decided on using Fragments, but I'm not sure if I can use those in already existing activities. All the examples I found focus on completely new projects, and not already existing ones. So in short, can you create fragments and use them in already existing activities? (such as showing the music player on top of the existing agenda activity)

推荐答案

我建议您按照以下方式进行.由于必须在所有活动中保留音乐播放状态,因此您必须使用服务来实现这一点.您还需要一个控制面板,它必须与活动分开存在.最好的方法是使用 fragments.创建一个连接到服务并提供 UI 来控制它的片段,然后将此片段添加到您需要的所有活动中.

I suggest you doing it the following way. Since music playing state must be retained in all the activities, you have to use a service to achieve this. Also you need a control panel, which must exist separately from activities. The best method to do it is by using fragments. Create a fragment which connects to the service and provides an UI to control it, then add this fragment to all the activities where you need it.

这篇关于在所有活动中显示一个共同的音乐播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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