gpt4 book ai didi

iphone - 如何在 subview 中添加 AVQueuePlayer 以显示 View

转载 作者:行者123 更新时间:2023-11-29 10:59:26 25 4
gpt4 key购买 nike

我制作了一个 AVQueuePlayer 并添加了一些视频项。当我运行应用程序时,视频的声音播放但视频不显示。我知道视频不显示的原因,这是因为我不知道如何在 View 中添加 AVQueuePlayer。请帮助我

NSString *firstVideoPath = [[NSBundle mainBundle] 
pathForResource:@"3" ofType:@"m4v"];
NSString *secondVideoPath = [[NSBundle mainBundle]
pathForResource:@"2" ofType:@"m4v"];
NSString *thirdVideoPath = [[NSBundle mainBundle]
pathForResource:@"3" ofType:@"m4v"];


AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:


[NSURL fileURLWithPath:firstVideoPath]];
AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:

[NSURL fileURLWithPath:secondVideoPath]];
AVPlayerItem *thirdVideoItem = [AVPlayerItem playerItemWithURL:
[NSURL fileURLWithPath:thirdVideoPath]];

self.queuePlayer = [AVQueuePlayer queuePlayerWithItems:
[NSArray arrayWithObjects:firstVideoItem,
secondVideoItem,thirdVideoItem,nil]];
[self.playerView setPlayer:self.queuePlayer];

[self.queuePlayer play];

最佳答案

首先,抓取所有路径:

NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"m4v"];
NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"mp4"];
NSString *thirdVideoPath = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"m4v"];

AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:firstVideoPath]];
AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondVideoPath]];
AVPlayerItem *thirdVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:thirdVideoPath]];

初始化AVQueuePlayer:

AVQueuePlayer *queuePlayer = [AVQueuePlayer queuePlayerWithItems:@[firstVideoItem, secondVideoItem,thirdVideoItem]];

最后使用图层将所有电影显示为播放列表。

AVPlayerLayer *layer = [AVPlayerLayer layer];

[layer setPlayer:queuePlayer];
[layer setFrame:CGRectMake(10, 10, 300, 200)];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

[self.view.layer addSublayer:layer];

[queuePlayer play];

不要忘记添加框架:

#import <QuartzCore/QuartzCore.h>
#import "AVFoundation/AVFoundation.h"

关于iphone - 如何在 subview 中添加 AVQueuePlayer 以显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677249/

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