当iOS 4设备进入后台状态时,是否可以运行NSThread或NSTimer?
我正在编写一个音频应用程序,它使用 iPod 音乐播放器播放 iPod 音乐库中的媒体项目。
现在我知道 iPod 在后台时可以继续播放,但我还想在后台运行一个线程或计时器,其中选择器方法会定期检查音乐播放器的状态,以便应用程序可以应用规则,以便在曲目之间应用延迟,并在后台自动将新媒体集合加载到播放器中。
这可能吗?
如果没有的话,还有其他方法可以实现这个功能吗?
I am writing an audio application which plays media items from the iPod music library using the iPod music player.
Now I know it is possible for the iPod to carry on playing when it is in the background, but I want to also run either a Thread or a Timer in the background where a selector method periodically checks the state of the music player so that the app can apply rules in order to apply delays between tracks, and automatically load up new media collections into the player all while in the background.
Is this possible?
If not, is there any other method for achieving this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
诀窍是不要使用
MPMusicPlayerController
,因为当您的应用进入后台时,它实际上放弃了对 iPod 应用的控制(您可以通过关闭您的应用并打开 iPod 应用来检查这一点 )。对于我目前正在开发的音乐应用程序,我使用
AVAudioSession
和AVPlayer
(不是AVAudioPlayer
推出了自己的音乐播放器代码>)。我在播放器上实现了一个队列,以及一个为队列提供数据的数据源(类似于UITableViewDatasource
)。所有这些都在后台运行时运行良好,并且与 iPod 应用程序没有任何关联(并且作为额外的好处,您的应用程序图标会出现在多任务栏上的遥控器旁边)。如果您开发了类似的东西,那么当您向数据源询问队列中的下一个项目时,您可以查询您的规则集并决定下一步要做什么。
The trick is to not use
MPMusicPlayerController
as when your app enters the background it actually relinquishes control to the iPod app (you can check this by closing your app and opening the iPod app).For the music app I'm currently developing I've rolled my own music player using
AVAudioSession
andAVPlayer
(notAVAudioPlayer
). I've implemented a queue on the player, and a datasource (similar toUITableViewDatasource
) which feeds the queue. All of this works eloquently whilst running in the background and has no association what ever with the iPod app (and as an added bonus you apps icon appears next to the remote controls on mulitasking bar).If you developed something similar then when you ask your datasource for the next item in the queue, you could query your rule set and take a decision on what to do next.
当然。您可以在应用程序委托的
applicationDidEnterBackground:
中执行此操作。从模板:
Sure. You can do this in the application delegate's
applicationDidEnterBackground:
.From the template:
是的,现在您无法使用
NSTimer
因为它们已失效。您可以通过 GCD 启动一段代码来在后台运行,该代码块的最大数量是。执行时间10分钟
Well yes and now, you can't use
NSTimer
because they are invalidated.You can start a block of code to run via GCD to run in background which will have the max. execute time of 10 min.