Android背景音乐服务
我正在 Android 中开发一个娱乐应用程序。我想播放背景音乐,并且我想为此使用服务。应用程序有 3 个活动,所有活动都必须播放音乐。此外,当活动暂停时,音乐必须暂停并在被破坏时停止。谁能告诉我该怎么做?有链接或例子吗?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
无需服务即可实现
https://web.archive.org/web/20181116173307/http://www.rbgrn.net/content/307-light-racer-20-days-61-64-completion
如果您真的想使用媒体播放器通过服务实现这一点,
请在清单中调用此服务
确保 .BackgroundSoundService 字符串末尾没有空格
Do it without service
https://web.archive.org/web/20181116173307/http://www.rbgrn.net/content/307-light-racer-20-days-61-64-completion
If you are so serious about doing it with services using mediaplayer
Please call this service in Manifest
Make sure there is no space at the end of the .BackgroundSoundService string
对于这里的聚会来说太晚了,但我仍然会添加 0.02 美元,Google 发布了一个名为通用音乐播放器的免费示例,您可以使用它来学习在它使用的所有 Android 平台(汽车、手表、移动设备、电视...)上播放音乐在后台播放音乐的服务,请检查一下,非常有帮助。这是该项目的链接
https://github.com/googlesamples/android-UniversalMusicPlayer
way too late for the party here but i will still add my $0.02, Google has released a free sample called universal music player with which you can learn to stream music across all android platforms(auto, watch,mobile,tv..) it uses service to play music in the background, do check it out very helpful. here's the link to the project
https://github.com/googlesamples/android-UniversalMusicPlayer
当使用
Service
和MediaPlayer
类时,@Synxmax 的答案是正确的,但是您还需要在清单中声明Service
才能正常工作,像这样:@Synxmax's answer is correct when using a
Service
and theMediaPlayer
class, however you also need to declare theService
in the Manifest for this to work, like so:我运行它时遇到问题,我做了一些更改以使用 mp3 源运行它。这是
BackfrounSoundService.java
文件。考虑一下我的 mp3 文件位于手机的 SD 卡中。i had problem to run it and i make some changes to run it with mp3 source. here is
BackfrounSoundService.java
file. consider that my mp3 file is in my sdcard in my phone .HelloAndroid 有一个关于这个主题的优秀教程。主题。事实上,这是我在谷歌上得到的第一个点击。在此处询问之前,您应该尝试使用谷歌搜索,因为这是一个很好的做法。
Theres an excellent tutorial on this subject at HelloAndroid regarding this very subject. Infact it was the first hit i got on google. You should try googling before asking here, as it is good practice.
使用
START_STICKY
标志创建前台服务。然后可以从任何活动调用它来播放一些音乐
您可以使用 bindService 并通过相应的 Activity 生命周期方法使 Service 暂停/停止。
这是一个关于在 Android 上在后台播放音乐< /a>
Create a foreground service with the
START_STICKY
flag.This can then be called from any activity to play some music
You can bind to the service using bindService and to make the Service pause/stop from the corresponding activity lifecycle methods.
Here's a good tutorial about Playing music in the background on Android
我找到了两个很棒的资源可以分享,如果其他人通过谷歌遇到这个线程,这可能会对他们有帮助(2018)。
其中一个是这个视频教程,您将在其中看到服务的实际工作原理,这对于初学者来说很有用。
链接:- https://www.youtube.com/watch?v=p2ffzsCqrs8
另一个是这个网站,它将真正帮助您使用背景音频播放器。
链接:- https://www.dev2qa。 com/android-play-audio-file-in-background-service-example/
祝你好运:)
I have found two great resources to share, if anyone else come across this thread via Google, this may help them ( 2018 ).
One is this video tutorial in which you'll see practically how service works, this is good for starters.
Link :- https://www.youtube.com/watch?v=p2ffzsCqrs8
Other is this website which will really help you with background audio player.
Link :- https://www.dev2qa.com/android-play-audio-file-in-background-service-example/
Good Luck :)