我有无法诊断的内存泄漏。我尝试了多种方法来创建无缝循环的视频 - 除了 AVPlayerLooper,我遇到并尝试过的所有方法都涉及创建一个观察者来观看AVPlayerItemDidPlayToEndTimeNotification
,然后寻找视频的开头(在 AVPlayer 的情况下)或插入要循环到视频队列中的视频(在 AVQueuePlayer 的情况下)。两者似乎具有相似的性能,但都具有与 seekToTime 方法(在 AVPlayer 的情况下)和 insertItem 方法(在 AVQueuePlayer 的情况下)相关的一致内存保持。我的最终目标是创建一个默认循环的 SKVideoNode 子类。下面是我的子类代码:
#import "SDLoopingVideoNode.h"
#import <AVFoundation/AVFoundation.h>
@interface SDLoopingVideoNode()
@property AVQueuePlayer *avQueuePlayer;
@property AVPlayerLooper *playerLooper;
@end
@implementation SDLoopingVideoNode
-(instancetype)initWithPathToResource:(NSString *)path withFiletype:(NSString *)filetype
{
if(self == [super init])
{
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:path ofType:filetype];
NSURL *videoURL = [NSURL fileURLWithPath:resourcePath];
AVAsset *videoAsset = [AVAsset assetWithURL:videoURL];
AVPlayerItem * videoItem = [AVPlayerItem playerItemWithAsset:videoAsset];
self.avQueuePlayer = [[AVQueuePlayer alloc] initWithItems:@[videoItem]];
NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
[noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
AVPlayerItem *video = [[AVPlayerItem alloc] initWithURL:videoURL];
[self.avQueuePlayer insertItem:video afterItem:nil];
NSLog(@"Video changed");
}];
self = (SDLoopingVideoNode*)[[SKVideoNode alloc] initWithAVPlayer: self.avQueuePlayer];
return self;
}
return nil;
}
@end
下面是在 didMoveToView 中初始化子类的方式:
SDLoopingVideoNode *videoNode = [[SDLoopingVideoNode alloc]initWithPathToResource:@"147406" withFiletype:@"mp4"];
[videoNode setSize:CGSizeMake(self.size.width, self.size.height)];
[videoNode setAnchorPoint:CGPointMake(0.5, 0.5)];
[videoNode setPosition:CGPointMake(0, 0)];
[self addChild:videoNode];
[videoNode play];