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

标签 iphone ios objective-c avplayer

我制作了一个 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/

相关文章:

ios - 什么是最有效的保存和加载静态字符串列表

ios - UITableView 底部不需要的黑色单元格

objective-c - 滚动表格 View 时点击后退按钮时应用程序崩溃

ios - 以编程方式更改 UIView 的位置

iphone - 是 NSDateFormatter dateFromString : supposed to return nil for an invalid string?

iphone - 使用 Objective c 访问 iPhone 中的用户推送通知首选项/权限

iphone - iOS facebook 整合方法

iphone - 当部分为空时,分组的 UITableView 显示空白

iphone - 在没有应用商店提交的情况下更新 ios 应用程序

iphone - 如何对 UIImage 进行去饱和处理?