gpt4 book ai didi

ios - 如何在不耗尽电池的情况下监控 MPMoviePlayerController 播放进度?

转载 作者:可可西里 更新时间:2023-11-01 03:49:52 25 4
gpt4 key购买 nike

我有一个使用 MPMoviePlayerController 播放音乐的媒体播放器应用程序。我需要根据播放位置更新 UI。据我所知,没有办法通过回调或其他方式主动从玩家那里接收此信息,我基本上需要自己轮询。

所以我想我会使用一个简单的计时器,每秒运行一次。代码是这样的:

设置代码中的某处:

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updatePlaybackProgressFromTimer:) userInfo:nil repeats:YES];

然后:

- (void) updatePlaybackProgressFromTimer:(NSTimer *)timer {

if (([UIApplication sharedApplication].applicationState == UIApplicationStateActive) && (player.playbackState == MPMoviePlaybackStatePlaying)) {

CGFloat progress = player.currentPlaybackTime / player.duration;
// do something useful with this info
}

计时器每秒运行一次,即使应用程序在后台也是如此。该方法首先查看应用是否处于事件状态以及播放器是否正在播放,然后进行一些 UI 更新。

以这种方式每秒运行一个计时器是否对电池生命周期有影响?我是否应该更勤奋,并尝试在进入后台时拆除计时器并在激活应用程序时重新激活它?我确定会有一些电池生命周期影响,但实际上,它有多严重?或者有没有其他推荐的方法来做这种事情?

最佳答案

我无法想象使用 NSTimer 会显着影响电池生命周期 - 除非它被触发时正在完成的工作会影响电池生命周期。计时器只是被添加到当前运行循环中:

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed.

NSTimer Class Reference

根据文档,当您的应用程序即将退出其事件状态时,您应该暂停任何计时器:

In response to this change, your app should do the following in its applicationWillResignActive: method:

  • Stop timers and other periodic tasks.
  • Stop any running metadata queries.
  • Do not initiate any new tasks.
  • Pause movie playback (except when playing back over AirPlay).
  • Enter into a pause state if your app is a game.
  • Throttle back OpenGL ES frame rates.
  • Suspend any dispatch queues or operation queues executing non-critical code. (You can continue processing network requests and other time-sensitive background tasks while inactive.)

When your app is moved back to the active state, its applicationDidBecomeActive: method should reverse any of the steps taken in the applicationWillResignActive: method. Thus, upon reactivation, your app should restart timers, resume dispatch queues, and throttle up OpenGL ES frame rates again. However, games should not resume automatically; they should remain paused until the user chooses to resume them.

iOS App Programming Guide

关于ios - 如何在不耗尽电池的情况下监控 MPMoviePlayerController 播放进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10877305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com