BinDev
BinDev

Reputation: 671

Loop Video in View

I am new so hang in there.

I am following this tutorial: https://www.youtube.com/watch?v=5iHglLK957s

There are loads of examples for looping a video in ios but can't seem to get a video to loop with a xib using macos Obj-C.

I created the Cocoa C Class and it creates the .h .m and .xib. I create an IB action from the .xib view in the .h and name it video view.

@property (assign) IBOutlet NSView *videoview;

In the .M I add:

#import "VideoTest.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

@interface VideoTest ()
{
    AVPlayer *player;
}
@end

@implementation VideoTest

- (void)windowDidLoad {
    [super windowDidLoad];

    NSString *strpath=[[NSBundle mainBundle]pathForResource:@"TestVideo" ofType:@"mp4"];
    NSURL *URL=[NSURL fileURLWithPath:strpath];
    AVPlayerItem *item=[AVPlayerItem playerItemWithURL:URL];
    player=[AVPlayerItem playerWithPlayerItem:item];
    player.volume=2.0;
    AVPlayerLayer *layer1=[AVPlayerLayer playerLayerWithPlayer:player];
    layer1.videoGravity=AVLayerVideoGravityResizeAspectFill;
    layer1.frame=_videoview.layer.bounds;
    [_videoview.layer addSublayer:layer1];
    [player play];
}

@end

TestVideo.mp4 is added to the project. Also added AVKit and AVfoundation.

When built, the xib window is just blank.

What I am really trying to do is simply seamless loop a video in a xib using MacOS and Obj-C. Can't seem to get it.

Any suggestions? Seems like looping a video in a window should be easy but it's stumping me hard in MacOS.

Upvotes: 0

Views: 153

Answers (1)

Vladimir Vodolazkiy
Vladimir Vodolazkiy

Reputation: 564

Current X Code allows to play AVPlayerView directly into the XIB, and after AVPlayer is created, just assign player property of view to your player. No need to make tricks with layers now :-)

Simple complete example is worth a thousand of words, I assume. So I prepared small project:

https://github.com/Voldemarus/VideoPlayerDemo/tree/master

Upvotes: 1

Related Questions