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

Posted

技术标签:

【中文标题】使用 URL 在 MPMoviePlayerController 中播放视频 - iOS【英文标题】:To play video in MPMoviePlayerController using URL - iOS 【发布时间】:2015-05-25 12:20:51 【问题描述】:

我正在尝试在 ios 中播放视频 - MPMoviePlayerController 使用 URL。但它不允许我加载视频。我在 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 播放视频吗???

请帮忙。

感谢和问候。

【问题讨论】:

查看***.com/questions/27443311/… 你是在虚拟机上运行它吗? 我只在模拟器上测试它,而不是在 VM 上,而是在 Mac mini 上。感谢您的宝贵意见。我现在就在设备上试试 【参考方案1】:

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

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

【讨论】:

以上是关于使用 URL 在 MPMoviePlayerController 中播放视频 - iOS的主要内容,如果未能解决你的问题,请参考以下文章

在url中如果访问本地文件将使用file协议?

Django - 在设置中使用反向 url 映射

使用 htaccess 重写后,如何在 PHP 中获取可见 URL(不是实际 URL)? [复制]

如何使用 PHP 在 URL 中传递 URL(作为 GET 参数)?

Python requests.get(URL) 在使用带点的 URL 时返回 404 错误

在URL中使用另一个url作为参数时会被`&`截断的问题