Android的,我应该做的MediaPlayer服务? [英] Android, should I make MediaPlayer a service?

查看:118
本文介绍了Android的,我应该做的MediaPlayer服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个Android应用程序,这将使用户能够收听广播电台。

I'm trying to write an Android application which will allow users to listen to a radio station.

我有开始和停止按钮的工作,并播放流。如果我preSS home键和开始做其他零零碎碎的电话,流继续播放。这是我想要的工作。

I have got the start and stop buttons to work and it plays the stream. If I press the home key and start doing other bits and pieces with the phone, the stream continues to play. This is how I want it to work.

如果我招谁惹创建服务打流,或者是我已经使用不够好方法?有什么好处将有它的服务带来了什么?大量的教程和例子网上似乎使用服务,但在我看来,它只是增加了额外的并发症到code。或者是更有效率?

Should I bother creating a service to play the stream, or is the method I already use good enough? What benefit would having it in a service bring? A lot of tutorials and examples online seem to use services but it seems to me it's just adding extra complications into the code. Or is it more efficient?

问候, 大卫

推荐答案

您不需要使用服务,但你的用户可能会在正常使用下发现,回放意外停止,这取决于正常使用的情况下,这样一个服务是最好的办法。

You don't need to use a service, but your users may under normal use find that playback stops unexpectedly, it depends on the normal use case, so a Service is the best approach.

的原因是由于在活性的,你的流对象/媒体播放器被当前连接到的寿命。

The reason is due to the lifetime of the Activity that your streaming object / media player is currently attached to.

目前一些点系统将完全释放活性现在,它不再是可见的用户,而一旦发生的GC播放就会停止。这将取决于存储pressure,哪些用户执行其它操作,例如启动其他应用程序发生。服务允许更长的寿命,一般他们的所有其他中背景活动被释放,但在前台活动前,后,将只释放。围绕服务流程生命周期规则的更完整的描述可以在Android文档找到这里

At some point the system will completely release the Activity now that it is no longer visible to the user, and once GC happens playback will stop. This will happen depending on memory pressure and what other actions the user takes, such as launching other apps. Services are allowed a longer life span, in general they will be released only after all other backgrounded activities are released but before the foreground activity. A more complete description of process lifecycle rules around Services can be found in the Android docs here.

您可能也想看看电源管理器和使用PARTIAL_WAKE_LOCK,这将让您在播放继续当手机通常会睡(在电池寿命为代价)。

You may also want to look at the PowerManager and the use of PARTIAL_WAKE_LOCK, which would allow playback to continue when the handset would normally sleep (at the cost of battery life).

这篇关于Android的,我应该做的MediaPlayer服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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