MacOS Obj-C无缝循环视频

问题描述 投票:0回答:1

我已经看到许多示例来无缝循环播放iOS中的视频,但是我发现在MacOS上没有很多可以使用的示例。我只是在尝试AVPlayer在窗口中循环播放视频。我可以播放视频并进行循环播放,但无法无缝播放。

我有一个示例,该示例循环播放3秒钟的视频,您可以清楚地看到它不是无缝循环。

我知道有一个AVPlayerLooper,但我很新,不知道如何实现。

#import "AppDelegate.h"
#import "VideoWindow1.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

@interface AppDelegate ()

@property (weak) IBOutlet AVPlayerView *playerView;
@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSWindow *VideoWindow;

@end

@implementation AppDelegate

AVPlayer *player;
BOOL loopPlayer;

- (void) movieEndDetected:(NSNotification *) note
{
    if (loopPlayer) {
        [player seekToTime:kCMTimeZero];
        [player play];
    }
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    loopPlayer = YES;

    // set up player
    NSBundle *mb = [NSBundle mainBundle];
    NSURL *demoURL = [mb URLForResource:@"Video2" withExtension:@"mp4"];
    player = [[AVPlayer alloc] initWithURL:demoURL];
    self.playerView.player = player;
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(movieEndDetected:)
               name:@"AVPlayerItemDidPlayToEndTimeNotification"
             object:player.currentItem];
            [player play];
    [_VideoWindow setAspectRatio:NSMakeSize(16, 9)];
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

@end
objective-c xcode macos cocoa avplayer
1个回答
0
投票

我能够使它与avlooper无缝循环。

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

@interface VideoWindow ()

@property (strong) IBOutlet AVPlayerView *playerView;
@property (strong) IBOutlet NSWindow *aspectView;

@end

@implementation VideoWindow

{
    AVPlayerItem *_playerItem;
    AVQueuePlayer *_player;
    AVPlayerLooper *_playerLooper;
    AVPlayerLayer *_playerLayer;
}


- (void)windowDidLoad {
    [super windowDidLoad];
    NSString *videoFile = [[NSBundle mainBundle] pathForResource:@"Video2" ofType:@"mp4"];
    NSURL *videoURL = [NSURL fileURLWithPath:videoFile];
    _playerItem = [AVPlayerItem playerItemWithURL:videoURL];
    _player = [AVQueuePlayer queuePlayerWithItems:@[_playerItem]];
    _playerLooper = [AVPlayerLooper playerLooperWithPlayer:_player templateItem:_playerItem];
    _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    _playerLayer.frame = self.playerView.bounds;
    _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.playerView.layer addSublayer:_playerLayer];
    [_aspectView setAspectRatio:NSMakeSize(16, 9)];
    [_player play];
}



- (void)windowWillClose:(NSNotification *)aNotification {
    [_player pause];
    [_player seekToTime:kCMTimeZero];
}

@end
© www.soinside.com 2019 - 2024. All rights reserved.