【问题标题】:How to play all local videos consecutively如何连续播放所有本地视频
【发布时间】:2017-12-14 10:52:41
【问题描述】:

我的应用通过点击 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];
}

【问题讨论】:

标签: ios objective-c xcode avfoundation avplayer


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 2015-04-23
    • 2020-09-07
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多