简体   繁体   English

如何在子视图中添加AVQueuePlayer以显示视图

[英]How to add AVQueuePlayer in subview to show view

I make a AVQueuePlayer and add some video items . 我制作了一个AVQueuePlayer并添加了一些视频项目。 when i run app , video's sound play but video is not showing. 当我运行app时,会播放视频的声音,但未显示视频。 i know the reason why video is not showing , it is because i haven't know how to add AVQueuePlayer in the view. 我知道视频未显示的原因,这是因为我不知道如何在视图中添加AVQueuePlayer。 Please help me 请帮我

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];   

First, grap all your paths: 首先,掌握所有路径:

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]];

Init AVQueuePlayer: 初始化AVQueuePlayer:

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

And finally use layer to display all movies as playlist. 最后使用图层将所有电影显示为播放列表。

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];

Don't forget to add frameworks: 不要忘记添加框架:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM