如何连续播放所有本地视频

How to play all local videos consecutively

我的应用程序通过点击 5 个不同的按钮播放 5 个短视频(存储在应用程序中)。我想要一个可以连续播放所有视频的 "Play all" 按钮。

这是我播放一个视频的代码:

-(IBAction)playVideo1;
{
NSURL *videoURL = [[NSBundle mainBundle]URLForResource:@”video1” withExtension:@"mp4"];

AVPlayer *player = [AVPlayer playerWithURL:videoURL];

AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
[player play];
[self presentViewController:controller animated:YES completion:nil];

controller.view.frame = self.view.frame;

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:_currentItem];
}

感谢提供的链接和一些谷歌搜索,我自己找到了答案。

以下是使用 AVQueuePlayer 按顺序播放视频的方法:

- (IBAction)playAll:(id)sender {
NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:@”video1” ofType:@"m4v"];
NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"video2” ofType:@"m4v"];
NSString *thirdVideoPath = [[NSBundle mainBundle] pathForResource:@"video3” ofType:@"m4v"];
NSString *fourthVideoPath = [[NSBundle mainBundle] pathForResource:@"video4” ofType:@"m4v"];
NSString *fifthVideoPath = [[NSBundle mainBundle] pathForResource:@"video5” ofType:@"m4v"];    

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

queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem, secondVideoItem, thirdVideoItem, fourthVideoItem, fifthVideoItem, nil]];

// create a player view controller
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = queuePlayer;
[self presentViewController:controller animated:YES completion:nil];
[queuePlayer play];