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

时间:2017-12-14 10:52:41

标签: ios objective-c xcode avfoundation avplayer

我的应用程序通过点击5个不同的按钮播放5个短视频(存储在应用程序中)。我希望有一个“全部播放”按钮,可以连续播放所有视频。

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

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

1 个答案:

答案 0 :(得分:0)

由于提供的链接和一些googleing,我设法找到了答案。

以下是使用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];