在我的iOS应用程序中,我在iOS YouTube helper library的帮助下将YouTube视频作为循环运行。但是我没有机会以全长播放视频,但是在20秒之后,我又像下面一样排队了同样的视频。
- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state{
if (state == kYTPlayerStateQueued) {
startedTimer = NO;
[self.playerView playVideo];
} else if (state == kYTPlayerStatePlaying) {
if (!startedTimer) {
startedTimer = YES;
vidReplayTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(restartVideo) userInfo:nil repeats:NO];
}
}
}
和
- (void)restartVideo {
[self.playerView cueVideoById:selectedYTVideoId startSeconds:0.1 suggestedQuality:kYTPlaybackQualityMedium];
}
这完全符合我的要求。
接下来我想在YouTube重播视频之前播放mp4文件。为此,我使用了AVPlayer。然后代码改变如下。
- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state{
if (state == kYTPlayerStatePlaying) {
if (self.avPlayer != nil) {
avPlayerLayer.hidden = YES;
self.avPlayer = nil;
}
if (!startedTimer) {
startedTimer = YES;
vidReplayTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(restartVideo) userInfo:nil repeats:NO];
}
}
}
和
- (void)restartVideo {
self.avPlayer = [AVPlayer playerWithURL:introVideoFileURL];
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
avPlayerLayer.frame = CGRectMake(0, 0, 320, 330);
[self.view.layer addSublayer: avPlayerLayer];
[self.avPlayer play];
[self.playerView cueVideoById:selectedYTVideoId startSeconds:0.1 suggestedQuality:kYTPlaybackQualityMedium];
}
经过上述更改后,应用程序按照我的预期运行,但运行将近四分钟,在我的Xcode和应用程序崩溃中弹出“由于内存压力而终止”弹出窗口。我已经使用Instruments开发人员工具检查了内存,应用程序在崩溃时使用了近35 MB的内存。当应用程序开始运行时,它使用了超过45 MB的内存并且运行顺畅。
正如您所看到的,我只在我需要它时创建AVPlayer并在完成工作后将其设置为nil。这个问题可能是什么原因,我该如何解决?
答案 0 :(得分:0)
将Bhavesh Lathigara在comments的解决方案移至社区Wiki回答:
调用
@return {DailyCountersNode}
时,取消与视频/音频播放相关的所有对象,并在需要时重新创建它们。