首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无缝环路视频

无缝环路视频
EN

Stack Overflow用户
提问于 2020-05-12 13:38:56
回答 1查看 134关注 0票数 0

我已经看到了很多在iOS中无缝循环视频的例子,但是我没有发现在MacOS中有多少可以工作的地方。我只是在实验AVPlayer在窗口中循环播放视频。我可以让视频播放和循环,但我不能让它无缝。

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

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

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-14 15:52:07

我能让这个和阿夫洛珀无缝地循环。

代码语言:javascript
复制
#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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61753199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档