在iOS 10中,AVPlayerLooper被添加到AVQueuePlayer中的循环视频中。我已经查看了官方的Apple文档,但它根本不清楚 - 最重要的是,提供的示例项目只是很快。我想知道如何在objective-c中运行AVPlayerLooper。以下是Apple文档供参考:https://developer.apple.com/reference/avfoundation/avplayerlooper?language=objc
我的最终目标是将其用作SKVideoNode子类的一部分。以下是使用AVPlayerLooper的子类的代码:
#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]];
self.playerLooper =[AVPlayerLooper playerLooperWithPlayer:self.avQueuePlayer templateItem:videoItem];
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];