私の iOS アプリケーションでは、iOS YouTube ヘルパー ライブラリを使用して、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];
}
上記の変更後、アプリは期待どおりに実行されますが、4 分近く実行すると、Xcode に「Terminated due to Memory Pressure」というポップアップ ウィンドウが表示され、アプリがクラッシュします。Instruments 開発者ツールでメモリを確認したところ、アプリがクラッシュするまでに約 35 MB のメモリが使用されました。アプリが実行を開始するまでに、45 MB を超えるメモリを使用し、スムーズに実行されます。
ご覧のとおり、必要なときにのみ AVPlayer を作成し、作業が完了した直後に nil に設定しています。この問題の原因は何ですか?どうすれば解決できますか?