hal*_*llo 13 objective-c ios avplayer
当使用AVPlayer从网址播放音频时,它将停止播放,例如断开与wifi的连接.
[player play];
Run Code Online (Sandbox Code Playgroud)
不恢复AVPlayer
player.rate // Value is 1.0
player.currentItem.isPlaybackLikelyToKeepUp // Value is YES
player.status // Value is AVPlayerStatusReadyToPlay
player.error // Value is nil
Run Code Online (Sandbox Code Playgroud)
但播放器没有播放任何音频.
如何处理与AVPlayer的断开连接,重新连接AVPlayer并重新开始播放?
0xc*_*ced 12
为了处理网络更改,您必须添加一个观察者AVPlayerItemFailedToPlayToEndTimeNotification
.
- (void) playURL:(NSURL *)url
{
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemFailedToPlayToEndTime:) name:AVPlayerItemFailedToPlayToEndTimeNotification object:playerItem];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
[self.player play];
}
- (void) playerItemFailedToPlayToEndTime:(NSNotification *)notification
{
NSError *error = notification.userInfo[AVPlayerItemFailedToPlayToEndTimeErrorKey];
// Handle error ...
}
Run Code Online (Sandbox Code Playgroud)
您应该为AVPlayerItemPlaybackStalledNotification添加观察者。
\n\nAVPlayerItemFailedToPlayToEndTimeNotification在这个问题上对我来说没有任何价值。
\n\n[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStalled:) name:AVPlayerItemPlaybackStalledNotification object:trackItem];\n
Run Code Online (Sandbox Code Playgroud)\n\n正如文档所说,如果必要的流媒体未通过网络及时传送,基于文件的播放将不会继续。
\n\n\n\n\nnotification\xe2\x80\x99s 对象是 AVPlayerItem 实例,其播放无法继续,因为必需的流媒体未通过网络及时传送\xe2\x80\x99t\n。一旦传送了足够的数据量,就会继续播放流媒体。\n 基于文件的播放不会继续。
\n
这解释了为什么 AVPlayer 可以在网络切换后恢复 HLS 流,但如果我使用 AVPlayer 播放基于文件的 TuneIn 资源则无法执行相同的操作。
\n\n那么答案就变得简单了。
\n\n- (void)playbackStalled:(NSNotification *)notification {\n if ([self isFileBased:streamUri]) {\n // Restart playback\n NSURL *url = [NSURL URLWithString:streamUri];\n AVPlayerItem *trackItem = [AVPlayerItem playerItemWithURL:url];\n AVPlayer *mediaPlayer = [AVPlayer playerWithPlayerItem:trackItem];\n [self registerObservers:trackItem player:mediaPlayer];\n [mediaPlayer play];\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n进一步阅读有关automaticWaitsToMinimizeStalling 的讨论。
\n 归档时间: |
|
查看次数: |
10108 次 |
最近记录: |