迭代过多的视频iOS

时间:2014-10-12 03:35:09

标签: ios objective-c arrays mpmovieplayercontroller

我有一系列的网址扩展程序。目标是分段的:

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

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

  3. 当MPMovieFinishReasonPlaybackEnded = TRUE或按下下一个按钮时,重复数组中的下一个网址扩展

  4. 这是我迄今为止的工作:

    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循环遍历数组中所有元素的每个操作,然后继续循环中包含的下一个操作。

    我所拥有的并不是产生所需的输出,我很感激任何反馈或建议。

1 个答案:

答案 0 :(得分:1)

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

-(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时再次调用它。