简体   繁体   中英

How to add AVQueuePlayer in subview to show view

I make a AVQueuePlayer and add some video items . when i run app , video's sound play but video is not showing. i know the reason why video is not showing , it is because i haven't know how to add AVQueuePlayer in the view. 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 *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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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