0

In iOS 10, AVPlayerLooper was added to loop videos in an AVQueuePlayer. I've viewed the official Apple documentation, but it is not clear at all - on top of that, the sample project provided is only in swift. I want to know how to run AVPlayerLooper in objective-c. Here is the Apple Documentation for reference: https://developer.apple.com/reference/avfoundation/avplayerlooper?language=objc

My end goal is to use this as part of an SKVideoNode subclass. Here is the code for the subclass using the 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

And then to initialize the object, I use this code in the "didMoveToView" method:

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];
Solsma Dev
  • 481
  • 6
  • 22
  • Seems pretty simple to me: Create an AVPlayerLooper using [AVPlayerLooper playerLooperWithPlayer:self.queuePlayer templateItem:playerItem];, then just tell your AVPlayer to play. –  Nov 16 '16 at 17:30
  • I've edited to the question to be more specific - the following code when initialized plays the video once, but does not loop as intended. – Solsma Dev Nov 16 '16 at 17:40
  • I am already initializing the AVPlayerLooper as `self.playerLooper =[AVPlayerLooper playerLooperWithPlayer:self.avQueuePlayer templateItem:videoItem];` – Solsma Dev Nov 16 '16 at 17:44
  • Just to check: Create a loop that stops when avQueuePlayer.isPlaying becomes false. Does it ever stop? –  Nov 28 '16 at 22:23

0 Answers0