gpt4 book ai didi

ios - AVPlayer 不在后台加载媒体

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:50 25 4
gpt4 key购买 nike

在后台运行时,我的 AVPlayer 实现无法播放下载的音频(例如播客),但能够播放本地存储的歌曲。无法在后台播放只有当手机与我的电脑断开连接时。如果我的手机直接连接到我的计算机/调试器,则任何本地媒体或下载的媒体都可以毫无问题地播放。在前台,播放任何一种媒体类型也没有问题。

这是我的实现:

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];
}
}

我有以下内容来处理通知中心通知:

- (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地址加载完毕,并用playerItem初始化AVPlayer后,一系列为 keyPath 发布通知:速率、playbackBufferEmpty、playbackLikelyToKeepUp 和状态。然后音频开始播放。显示各种 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

所以总而言之,您在上面看到当在前台或后台运行时,如果直接连接到计算机/调试器,AVPlayer 会成功加载播放缓冲区并播放音频。但是当在后台运行并且未连接到计算机/调试器时,播放器似乎没有加载媒体而只是挂起。

在所有情况下,都不会收到 AVPlayerItemPlaybackStalledNotification。

抱歉解释太长了。任何人都可以看到什么可能导致播放器在未连接到计算机时在后台挂起吗?

最佳答案

尝试在开始播放之前调用 beginBackgroundTaskdocumentation说它不应该用于在后台运行,但它也说它对未完成的业务很有用,我相信这描述了你的情况。

我的推理是这样的:

  1. 要让您的应用在 audio 后台模式下在后台运行,您必须在 iOS 通常为您取消安排时播放音频
  2. 当播放远程媒体时,从您告诉 AVPlayer 播放 到音频实际开始播放之间,它自然需要比本地媒体更多的时间,从而允许您的应用程序继续。

因此后台任务为您的应用提供更多时间 ( some say as much as 3 minutes ) 来加载远程媒体和播放音频。

你一定要这样做吗?可能不是 - 这是一个 AVPlayer/iOS 调度器实现细节 - 为了让 iOS 调度器在背景音频情况下“公平”,它真的应该识别你“尽力”播放音频的尝试让 AVPlayer.play() 标记背景音频的开始。如果由于某种原因播放失败,那么很好,取消安排。无论如何,如果您对此有强烈的感觉,您可以提交功能请求。

附注不要忘记调用 endBackgroundTask!无论是在过期处理程序中,还是为了成为一个优秀的移动设备公民,一旦音频开始播放,确保您的后台继续运行。如果 endBackgroundTask 影响了您在后台播放音频的能力,那么您就太早调用它了!

关于ios - AVPlayer 不在后台加载媒体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42707701/

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