gpt4 book ai didi

objective-c - 在 View 中循环播放视频

转载 作者:行者123 更新时间:2023-12-03 18:07:29 25 4
gpt4 key购买 nike

我是新人,所以坚持下去。

我正在关注本教程: https://www.youtube.com/watch?v=5iHglLK957s

有很多在 ios 中循环播放视频的示例,但似乎无法使用 macos Obj-C 使 xib 循环播放视频。

我创建了 Cocoa C 类,它创建了 .h .m 和 .xib。我从 .h 中的 .xib View 创建一个 IB 操作,并将其命名为视频 View 。

@property (assign) IBOutlet NSView *videoview;

在 .M 中我添加:

#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 已添加到项目中。还添加了 AVKit 和 AVfoundation。

构建后,xib 窗口只是空白。

我真正想做的只是使用 MacOS 和 Obj-C 在 xib 中无缝循环视频。好像看不懂。

有什么建议吗?看起来在窗口中循环播放视频应该很容易,但在 MacOS 中却很难了。

最佳答案

当前的X代码允许直接在XIB中播放AVPlayerView,并且在创建AVPlayer之后,只需将 View 的播放器属性分配给您的播放器即可。现在不需要用图层来搞花招了:-)

我认为简单的完整示例胜过一千个字。所以我准备了一个小项目:

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

关于objective-c - 在 View 中循环播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61622631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com