在iOS的视频数组上迭代



我有一个URL扩展数组。目标是分段的:

  1. 将扩展名添加到基本URL以创建特定视频的URL。

  2. 使用YTViewExtractor在MPMovieViewController中播放视频。

  3. 当MPMovieFinishReasonPlaybackEnded=TRUE或按下下一个按钮时,对数组中的下一个URL扩展重复此操作

这是我迄今为止的工作:

int i;
for (i=0; i < [_uriToBeAppended count]; i++)
{
    NSString *uriString = [_uriToBeAppended  objectAtIndex:i];
    NSString *urlString = [NSString stringWithFormat:@"http://vimeo.com/%@", uriString];
    NSLog(@"URL String: %@", urlString);
    [YTVimeoExtractor fetchVideoURLFromURL:urlString
                                   quality:YTVimeoVideoQualityMedium
                         completionHandler:^(NSURL *videoURL, NSError *error, YTVimeoVideoQuality quality) {
                             if (error) {
                                 // handle error
                                 NSLog(@"Video URL: %@", [videoURL absoluteString]);
                             } else {
                                 // run player
                                 self.moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
                                 [self.moviePlayer.moviePlayer prepareToPlay];
                                 [self presentViewController:self.moviePlayer animated:YES completion:nil];
                             }
                         }];
}

日志:

    2014-10-11 22:30:05.528 Voulette[668:162653] URL String: http://vimeo.com/96558506
.
.
2014-10-11 22:30:05.577 Voulette[668:162653] URL String: http://vimeo.com/6615855

2014-10-11 22:30:06.997 Voulette[668:162653] -[UIApplication beginIgnoringInteractionEvents] overflow. Ignoring.
.
.
2014-10-11 22:30:09.700 Voulette[668:162653] -[UIApplication beginIgnoringInteractionEvents] overflow. Ignoring.

2014-10-11 22:30:10.063 Voulette[668:162653] -[UIApplication endIgnoringInteractionEvents] called without matching -beginIgnoringInteractionEvents. Ignoring.
.
.
2014-10-11 22:30:10.189 Voulette[668:162653] -[UIApplication endIgnoringInteractionEvents] called without matching -beginIgnoringInteractionEvents. Ignoring.

2014-10-11 22:30:11.519 Voulette[668:162653] Warning: Attempt to present <MPMoviePlayerViewController: 0x1568fcd0> on <ViewController: 0x1554e3e0> whose view is not in the window hierarchy!
.
.
2014-10-11 22:30:11.729 Voulette[668:162653] Warning: Attempt to present <MPMoviePlayerViewController: 0x16818810> on <ViewController: 0x1554e3e0> whose view is not in the window hierarchy!

2014-10-11 22:30:11.739 Voulette[668:162653] -[UIApplication beginIgnoringInteractionEvents] overflow. Ignoring.
2014-10-11 22:30:11.742 Voulette[668:162653] Warning: Attempt to present <MPMoviePlayerViewController: 0x1681d690> on <ViewController: 0x1554e3e0> whose view is not in the window hierarchy!
.
.
2014-10-11 22:30:11.887 Voulette[668:162653] -[UIApplication beginIgnoringInteractionEvents] overflow. Ignoring.
2014-10-11 22:30:11.903 Voulette[668:162653] Warning: Attempt to present <MPMoviePlayerViewController: 0x157b1e70> on <ViewController: 0x1554e3e0> whose view is not in the window hierarchy!

2014-10-11 22:30:12.674 Voulette[668:162653] -[UIApplication endIgnoringInteractionEvents] called without matching -beginIgnoringInteractionEvents. Ignoring.
.
.
2014-10-11 22:30:12.699 Voulette[668:162653] -[UIApplication endIgnoringInteractionEvents] called without matching -beginIgnoringInteractionEvents. Ignoring.

日志表明,for循环在继续执行循环中包含的下一个操作之前,将遍历数组中所有元素的每个操作。

我所拥有的并没有产生想要的产出,我很感激任何反馈或建议。

由于电影的播放是一个异步事件,您的循环将在第一部电影几乎没有开始之前(可能在实际开始之前)一直执行。你需要把代码放在电影完成时调用的方法中,并使用计数器来跟踪要选择的url,而不是使用循环。

-(void)playNextMovie {
    static int i = 0;
    if (i < _uriToBeAppended.count) {
        NSString *uriString = [_uriToBeAppended  objectAtIndex:i];
        NSString *urlString = [NSString stringWithFormat:@"http://vimeo.com/%@", uriString];
        NSLog(@"URL String: %@", urlString);
        [YTVimeoExtractor fetchVideoURLFromURL:urlString
                                   quality:YTVimeoVideoQualityMedium
                         completionHandler:^(NSURL *videoURL, NSError *error, YTVimeoVideoQuality quality) {
                             if (error) {
                                 // handle error
                                 NSLog(@"Video URL: %@", [videoURL absoluteString]);
                             } else {
                                 // run player
                                 self.moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
                                 [self.moviePlayer.moviePlayer prepareToPlay];
                                 [self presentViewController:self.moviePlayer animated:YES completion:nil];
                             }
                         }];
        i++;
    }
}

调用此方法启动您的第一部电影,然后在调用MPMoviePlayerPlaybackDidFinishNotification时再次调用它。

最新更新