使用URL-iOS在MPMoviePlayerController中播放视频

时间:2015-05-25 12:20:52

标签: ios iphone video mpmovieplayercontroller

我正在尝试在iOS中播放视频 - 使用URL的MPMoviePlayerController。但它不允许我加载视频。我在viewDidLoad方法中使用以下代码:

NSURL *fileURL = [NSURL URLWithString:@"http://videos.testtube.com/revision3/web/discoverydinosaurs/0035/discoverydinosaurs--0035--male-dinosaurs-battle--large.h264.mp4"];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:self.view.bounds];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;
moviePlayerController.fullscreen = YES;
moviePlayerController.shouldAutoplay = YES;
[moviePlayerController prepareToPlay];
[moviePlayerController play];

它给了我以下错误日志:

<Info>: logging starts...
<Debug>: setMessageLoggingBlock: called
ERROR:     98: Error '!obj' trying to fetch default input device's sample rate
ERROR:     100: Error getting audio input device sample rate: '!obj'
WARNING:   230: The input device is 0x0; '(null)'
WARNING:   234: The output device is 0x26; 'AppleHDAEngineOutput:1B,0,1,2:0'
ERROR:     400: error '!obj'
ERROR:     400: error -66680
ERROR:     113: * * * NULL AQIONode object
ERROR:     400: error -66680
ERROR:     400: error -66680
ERROR:     703: Can't make UISound Renderer
ERROR:     400: error -66680
ERROR:     400: error -66680
ERROR:     400: error -66680
ERROR:     400: error -66680
ERROR:     400: error -66680

请帮我解决此错误。

是否真的可以在MPMoviePlayerController中使用URL播放视频???

请帮忙。

谢谢和问候。

1 个答案:

答案 0 :(得分:0)

它适用于您的网址。我试过了......

-(void)Play
{
    NSURL *videoPath = [NSURL URLWithString:@"http://videos.testtube.com/revision3/web/discoverydinosaurs/0035/discoverydinosaurs--0035--male-dinosaurs-battle--large.h264.mp4"];

    MPMoviePlayerViewController *mpViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoPath];
    // Remove the movie player view controller from the "playback did finish" notification observers
    [[NSNotificationCenter defaultCenter] removeObserver:mpViewController
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:mpViewController.moviePlayer];

    // Register this class as an observer instead
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:mpViewController.moviePlayer];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(doneButtonClick:)
                                                 name:MPMoviePlayerWillExitFullscreenNotification
                                               object:nil];

    // Set the modal transition style of your choice
    mpViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    // Present the movie player view controller
    [self presentViewController:mpViewController animated:YES completion:nil];


    // Start playback
    [mpViewController.moviePlayer prepareToPlay];
    [mpViewController.moviePlayer play];

}


- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Obtain the reason why the movie playback finished
    NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    // Dismiss the view controller ONLY when the reason is not "playback ended"
    if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
    {
        MPMoviePlayerController *moviePlayer = [aNotification object];

        // Remove this class from the observers
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];

        // Dismiss the view controller
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}