AVPlayer 不在后台加载媒体
AVPlayer Not Loading Media In Background
当 运行 在后台时,我的 AVPlayer 实现无法播放下载的音频(例如播客),但能够播放本地存储的歌曲。无法在后台播放只有当 phone 与我的计算机断开连接时。如果我的 phone 直接连接到我的 computer/debugger,则任何本地媒体或下载的媒体都可以正常播放。在前台,播放任何一种媒体类型也没有问题。
这是我的实现:
AVPlayer *moviePlayer;
AVPlayerItem *playerItem;
NSURL *address = /* the url of either a local song or remote media */
if (moviePlayer != nil) {
NSLog(@"removing rate, playbackBufferEmpty, playbackLikelyToKeepUp observers before creating new player");
[moviePlayer removeObserver:self forKeyPath:@"rate"];
[playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
[playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
[playerItem removeObserver:self forKeyPath:@"status"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemFailedToPlayToEndTimeNotification object:playerItem];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemPlaybackStalledNotification object:playerItem];
[self setMoviePlayer:nil]; // release and nilify
}
// The following block of code was an experiment to see if starting a background task would resolve the problem. As implemented, this did not help.
if([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive)
{
NSLog(@"Experiment. Starting background task to keep iOS awake");
task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
}];
}
playerItem = [[AVPlayerItem alloc]initWithURL:address];
moviePlayer = [[AVPlayer alloc]initWithPlayerItem:playerItem];
// Add a notification to make sure that the player starts playing. This is handled by observeValueForKeyPath
[moviePlayer addObserver:self
forKeyPath:@"rate"
options:NSKeyValueObservingOptionNew
context:nil];
[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
// The following 2 notifications handle the end of play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:AVPlayerItemFailedToPlayToEndTimeNotification
object:playerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackStalled:)
name:AVPlayerItemPlaybackStalledNotification
object:playerItem];
// Indicate the action the player should take when it finishes playing.
moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause;
moviePlayer.automaticallyWaitsToMinimizeStalling = NO;
更新:在上面的实现中,我还展示了启动后台任务的实验性尝试,希望 AVPlayer 能够在后台播放播客。这也无济于事,但我将其包含在内以供参考。未显示,但我也在 AVPlayerItem playbackLikelyToKeepUp 状态更改为 TRUE 后结束后台任务。
然后我有以下代码来处理 keyPath 通知:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"rate"]) {
NSString *debugString = [NSString stringWithFormat: @"In observeValueForKeyPath: rate"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
}
else if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"]) {
NSString *debugString = [NSString stringWithFormat: @"In observeValueForKeyPath: playbackBufferEmpty"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
}
else if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"]) {
NSString *debugString = [NSString stringWithFormat: @"In observeValueForKeyPath: playbackLikelyToKeepUp"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
}
else if (object == playerItem && [keyPath isEqualToString:@"status"]) {
NSString *debugString = [NSString stringWithFormat: @"In observeValueForKeyPath: status"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
}
}
我有以下方法来处理 notificationCenter 通知:
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSLog(@"moviePlaybackDidFinish. Time to stopMoviePlayerWithMusicPlayIndication");
[self stopMoviePlayer: YES]; // stop the movie player
}
- (void) moviePlayBackStalled:(NSNotification*)notification {
NSString *debugString = [NSString stringWithFormat: @"In moviePlayBackStalled. Restarting player"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
[moviePlayer play];
}
通过实现一个日志记录工具来跟踪与计算机断开连接时的执行情况,这是我观察到的:
当运行在后台与电脑断开连接时,playerItem加载url地址,AVPlayer用playerItem初始化。这会导致发布通知 observeValueForKeyPath: rate,但这是收到的最后一个通知。音频不播放。玩家只是挂起。我的日志输出显示了各种 moviePlayer 和 playerItem 标志,如下所示:
ready to play movie/podcast/song dedication/Song from url: http://feedproxy.google.com/~r/1019TheMix-EricAndKathy/~5/ZZnF09tuxr0/20170309-1_1718764.mp3
In observeValueForKeyPath: rate - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusUnknown, playbackToKeepUp: 0, playbackBufferEmpty: 1, playbackBufferFull: 0, rate: 1.0
但是后台直接连接电脑时运行或者前台运行时,从下面的日志输出可以看到在url地址后已加载并且 AVPlayer 已使用 playerItem 进行初始化,针对 keyPath 发布了一系列通知:rate、playbackBufferEmpty、playbackLikelyToKeepUp 和 status。然后音频开始播放。显示各种 moviePlayer 和 playerItem 标志的输出如下:
ready to play movie/podcast/song dedication/Song from url: http://feedproxy.google.com/~r/1019TheMix-EricAndKathy/~5/d3w52TBzd88/20170306-1-16_1717980.mp3
In observeValueForKeyPath: rate - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusUnknown, playbackToKeepUp: 0, playbackBufferEmpty: 1, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: playbackBufferEmpty - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusUnknown, playbackToKeepUp: 0, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: playbackLikelyToKeepUp - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusUnknown, playbackToKeepUp: 0, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: status - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusReadyToPlay, playbackToKeepUp: 0, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: playbackLikelyToKeepUp - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusReadyToPlay, playbackToKeepUp: 1, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: rate - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusReadyToPlay, playbackToKeepUp: 1, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
所以总而言之,您在上面看到当 运行 在前台或在后台直接连接到 computer/debugger 时,AVPlayer 成功加载播放缓冲区并播放音频。但是当 运行 在后台并且未连接到 computer/debugger 时,播放器似乎没有加载媒体而只是挂起。
在所有情况下,都不会收到 AVPlayerItemPlaybackStalledNotification。
抱歉解释得太长了。任何人都可以看到什么可能导致播放器在未连接到计算机时在后台挂起吗?
尝试在开始播放之前调用 beginBackgroundTask
。 documentation 说它不应该用于 运行 在后台,但它也说它对未完成的业务有用,我相信这描述了你的情况。
我的推理是这样的:
- 要让您的应用在
audio
后台模式下 运行 在后台运行,您 必须 在 iOS 通常取消安排的时间播放音频你
- 播放远程媒体时,从您告诉
AVPlayer
到 play
和音频实际开始播放之间,它自然需要比本地媒体更多的时间,让您的应用程序继续。
因此,后台任务会为您的应用提供更多时间 (some say as much as 3 minutes) 来加载远程媒体和播放音频。
你一定要这样做吗?可能不是 - 这是一个 AVPlayer
/iOS 调度器实现细节 - 在背景音频情况下 iOS 调度器是 "fair",它真的应该识别你的 "best effort" 尝试通过让 AVPlayer.play()
标记背景音频的开头来播放音频。如果由于某种原因播放失败,那么很好,取消安排。无论如何,如果您对此有强烈的感觉,可以提出功能请求。
p.s。别忘了打电话给 endBackgroundTask
!无论是在过期处理程序中,还是为了成为一名优秀的移动设备公民,一旦音频开始播放,确保您的持续后台 运行ning。如果 endBackgroundTask
影响了您在后台播放音频的能力,那么您就太早调用它了!
当 运行 在后台时,我的 AVPlayer 实现无法播放下载的音频(例如播客),但能够播放本地存储的歌曲。无法在后台播放只有当 phone 与我的计算机断开连接时。如果我的 phone 直接连接到我的 computer/debugger,则任何本地媒体或下载的媒体都可以正常播放。在前台,播放任何一种媒体类型也没有问题。
这是我的实现:
AVPlayer *moviePlayer;
AVPlayerItem *playerItem;
NSURL *address = /* the url of either a local song or remote media */
if (moviePlayer != nil) {
NSLog(@"removing rate, playbackBufferEmpty, playbackLikelyToKeepUp observers before creating new player");
[moviePlayer removeObserver:self forKeyPath:@"rate"];
[playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
[playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
[playerItem removeObserver:self forKeyPath:@"status"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemFailedToPlayToEndTimeNotification object:playerItem];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemPlaybackStalledNotification object:playerItem];
[self setMoviePlayer:nil]; // release and nilify
}
// The following block of code was an experiment to see if starting a background task would resolve the problem. As implemented, this did not help.
if([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive)
{
NSLog(@"Experiment. Starting background task to keep iOS awake");
task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
}];
}
playerItem = [[AVPlayerItem alloc]initWithURL:address];
moviePlayer = [[AVPlayer alloc]initWithPlayerItem:playerItem];
// Add a notification to make sure that the player starts playing. This is handled by observeValueForKeyPath
[moviePlayer addObserver:self
forKeyPath:@"rate"
options:NSKeyValueObservingOptionNew
context:nil];
[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
// The following 2 notifications handle the end of play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:playerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:AVPlayerItemFailedToPlayToEndTimeNotification
object:playerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackStalled:)
name:AVPlayerItemPlaybackStalledNotification
object:playerItem];
// Indicate the action the player should take when it finishes playing.
moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause;
moviePlayer.automaticallyWaitsToMinimizeStalling = NO;
更新:在上面的实现中,我还展示了启动后台任务的实验性尝试,希望 AVPlayer 能够在后台播放播客。这也无济于事,但我将其包含在内以供参考。未显示,但我也在 AVPlayerItem playbackLikelyToKeepUp 状态更改为 TRUE 后结束后台任务。
然后我有以下代码来处理 keyPath 通知:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"rate"]) {
NSString *debugString = [NSString stringWithFormat: @"In observeValueForKeyPath: rate"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
}
else if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"]) {
NSString *debugString = [NSString stringWithFormat: @"In observeValueForKeyPath: playbackBufferEmpty"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
}
else if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"]) {
NSString *debugString = [NSString stringWithFormat: @"In observeValueForKeyPath: playbackLikelyToKeepUp"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
}
else if (object == playerItem && [keyPath isEqualToString:@"status"]) {
NSString *debugString = [NSString stringWithFormat: @"In observeValueForKeyPath: status"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
}
}
我有以下方法来处理 notificationCenter 通知:
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSLog(@"moviePlaybackDidFinish. Time to stopMoviePlayerWithMusicPlayIndication");
[self stopMoviePlayer: YES]; // stop the movie player
}
- (void) moviePlayBackStalled:(NSNotification*)notification {
NSString *debugString = [NSString stringWithFormat: @"In moviePlayBackStalled. Restarting player"];
DLog(@"%@", debugString);
debugString = [self appendAvPlayerStatus: debugString];
[[FileHandler sharedInstance] logDebugString:debugString];
[moviePlayer play];
}
通过实现一个日志记录工具来跟踪与计算机断开连接时的执行情况,这是我观察到的:
当运行在后台与电脑断开连接时,playerItem加载url地址,AVPlayer用playerItem初始化。这会导致发布通知 observeValueForKeyPath: rate,但这是收到的最后一个通知。音频不播放。玩家只是挂起。我的日志输出显示了各种 moviePlayer 和 playerItem 标志,如下所示:
ready to play movie/podcast/song dedication/Song from url: http://feedproxy.google.com/~r/1019TheMix-EricAndKathy/~5/ZZnF09tuxr0/20170309-1_1718764.mp3
In observeValueForKeyPath: rate - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusUnknown, playbackToKeepUp: 0, playbackBufferEmpty: 1, playbackBufferFull: 0, rate: 1.0
但是后台直接连接电脑时运行或者前台运行时,从下面的日志输出可以看到在url地址后已加载并且 AVPlayer 已使用 playerItem 进行初始化,针对 keyPath 发布了一系列通知:rate、playbackBufferEmpty、playbackLikelyToKeepUp 和 status。然后音频开始播放。显示各种 moviePlayer 和 playerItem 标志的输出如下:
ready to play movie/podcast/song dedication/Song from url: http://feedproxy.google.com/~r/1019TheMix-EricAndKathy/~5/d3w52TBzd88/20170306-1-16_1717980.mp3
In observeValueForKeyPath: rate - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusUnknown, playbackToKeepUp: 0, playbackBufferEmpty: 1, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: playbackBufferEmpty - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusUnknown, playbackToKeepUp: 0, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: playbackLikelyToKeepUp - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusUnknown, playbackToKeepUp: 0, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: status - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusReadyToPlay, playbackToKeepUp: 0, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: playbackLikelyToKeepUp - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusReadyToPlay, playbackToKeepUp: 1, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
In observeValueForKeyPath: rate - timeCntrlStatus: AVPlayerTimeControlStatusPlaying, itemStatus: AVPlayerItemStatusReadyToPlay, playbackToKeepUp: 1, playbackBufferEmpty: 0, playbackBufferFull: 0, rate: 1.0
所以总而言之,您在上面看到当 运行 在前台或在后台直接连接到 computer/debugger 时,AVPlayer 成功加载播放缓冲区并播放音频。但是当 运行 在后台并且未连接到 computer/debugger 时,播放器似乎没有加载媒体而只是挂起。
在所有情况下,都不会收到 AVPlayerItemPlaybackStalledNotification。
抱歉解释得太长了。任何人都可以看到什么可能导致播放器在未连接到计算机时在后台挂起吗?
尝试在开始播放之前调用 beginBackgroundTask
。 documentation 说它不应该用于 运行 在后台,但它也说它对未完成的业务有用,我相信这描述了你的情况。
我的推理是这样的:
- 要让您的应用在
audio
后台模式下 运行 在后台运行,您 必须 在 iOS 通常取消安排的时间播放音频你 - 播放远程媒体时,从您告诉
AVPlayer
到play
和音频实际开始播放之间,它自然需要比本地媒体更多的时间,让您的应用程序继续。
因此,后台任务会为您的应用提供更多时间 (some say as much as 3 minutes) 来加载远程媒体和播放音频。
你一定要这样做吗?可能不是 - 这是一个 AVPlayer
/iOS 调度器实现细节 - 在背景音频情况下 iOS 调度器是 "fair",它真的应该识别你的 "best effort" 尝试通过让 AVPlayer.play()
标记背景音频的开头来播放音频。如果由于某种原因播放失败,那么很好,取消安排。无论如何,如果您对此有强烈的感觉,可以提出功能请求。
p.s。别忘了打电话给 endBackgroundTask
!无论是在过期处理程序中,还是为了成为一名优秀的移动设备公民,一旦音频开始播放,确保您的持续后台 运行ning。如果 endBackgroundTask
影响了您在后台播放音频的能力,那么您就太早调用它了!