ios MPMoviePlayerController 显示黑色空屏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8429701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:47:39  来源:igfitidea点击:

MPMoviePlayerController showing black empty screen

iosobjective-cvideompmovieplayercontroller

提问by Ritika

I use MPMoviePlayerControllerto play a local file in my Application Document folder which have I have downloaded for a server URL:

我曾经MPMoviePlayerController在我的应用程序文档文件夹中播放本地文件,该文件夹已为服务器 URL 下载:

itemMoviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
 [self.view addSubview:itemMoviePlayerController.view];
 itemMoviePlayerController.fullscreen = YES;
 itemMoviePlayerController.movieSourceType = MPMovieSourceTypeFile;
 itemMoviePlayerController.initialPlaybackTime = -1.0;
 [itemMoviePlayerController play];

When I play .movfile just after I downloaded it, It shows up a black empty screen & app UI is unusable. But if play same local file next time, it plays fine. I even verified playState& localStatefor MPMoviePlayerControllerthey seems fine. What could be reason for black empty screen?

当我.mov在下载文件后播放文件时,它显示一个黑色的空屏幕并且应用程序 UI 无法使用。但是如果下次播放相同的本地文件,则播放正常。我什至验证过playState&localState因为MPMoviePlayerController它们看起来不错。黑屏的原因是什么?

采纳答案by Ritika

This issue seems fixed after updating device iOS to 5.0.
Seems to iOS SDK issue for previous version

将设备 iOS 更新到 5.0 后,此问题似乎已解决。
似乎是以前版本的 iOS SDK 问题

回答by Toms Shaji Mathew

You need to retain your instance of MPMoviePlayerController i.e. as a property or an instance variable. The reference to the movie player is lost if you do not retain it.

您需要保留 MPMoviePlayerController 的实例,即作为属性或实例变量。如果您不保留对电影播放器​​的引用,它就会丢失。

回答by dulgan

You could try to put [itemMoviePlayerController prepareToPlay];before the [itemMoviePlayerController play];

你可以尝试把[itemMoviePlayerController prepareToPlay];[itemMoviePlayerController play];

The way preferred by Apple to display an only full screen video is to present a modal MPMoviePlayerViewController(as Hollance said). To present it, you should use something like :

Apple 首选的显示全屏视频的方式是呈现模式MPMoviePlayerViewController(如 Hollance 所说)。要呈现它,您应该使用以下内容:

 moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
 [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
 [itemMoviePlayerController play];

This is documented by Apple here

这是苹果在这里记录的

You can read there that you should keep a reference to your MPMoviePlayerViewControllerin order to dismiss it later with

您可以在那里读到您应该保留对您的引用MPMoviePlayerViewController,以便稍后将其关闭

[self dismissMoviePlayerViewControllerAnimated:moviePlayerViewController].

[self dismissMoviePlayerViewControllerAnimated:moviePlayerViewController].

回答by mattblessed

I fixed this by putting

我通过放置来解决这个问题

@property (strong, nonatomic) MPMoviePlayerController *moviePlayer; 

in my .h file, and calling self.moviePlayerin all my code and it worked!

在我的 .h 文件中,并调用self.moviePlayer我的所有代码,它起作用了!

回答by Hollance

You need to use MPMoviePlayerViewController instead. Notice the word "View" in there.

您需要改用 MPMoviePlayerViewController。请注意那里的“查看”一词。

回答by Jerin4info24x7

I hope this will help. I solved this problem in my project

我希望这将有所帮助。我在我的项目中解决了这个问题

 -(void)startPlayingMovie
{
    NSLog(@"startPlayingMovie");
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"Start_video" ofType:@"mov"]];
    moviePlayer =  [[MPMoviePlayerViewController alloc]
                    initWithContentURL:url];
    moviePlayer.view.frame = CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayer.moviePlayer];

moviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.moviePlayer.shouldAutoplay = YES;
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer.moviePlayer setFullscreen:YES animated:NO];

NSLog(@"endPlayingMovie");

}

回答by ghostatron

I had this same problem and it made me crazy. It would workon one view controller fine (audio and video), but not work on another (black screen with just audio). In the end, all I did was CHANGE THE ORDER of the calls: I simply waited until I was done configuring the movie player before adding it to the view. In other words, I called "addSubview" on the line just before the call to "play".

我遇到了同样的问题,这让我发疯了。它将工作在一个视图控制器罚款(音频和视频),但不工作的另一个(黑屏只是音频)上。最后,我所做的只是更改调用的顺序:我只是等到完成电影播放器​​的配置后再将其添加到视图中。换句话说,我在调用“play”之前在线上调用了“addSubview”。

回答by cayeric

Same thing happened to me. It turns out, I had tried to play the movie while the UIImagePickerwasn't dismissed yet.

同样的事情发生在我身上。事实证明,我曾尝试在UIImagePicker尚未被解雇的情况下播放这部电影。

In my UIImagePickerDelegateI had to first dismiss the UIImagePickerPopup and then open the ViewControllermanaging the MPMediaPlayerController:

在我的UIImagePickerDelegate我必须首先关闭UIImagePicker弹出窗口,然后打开ViewController管理MPMediaPlayerController

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)infoDict
{
    //the first thing to do: dismiss the media picker
    if ([ipPop isPopoverVisible])
    {
        [ipPop dismissPopoverAnimated:ANIMATION_SETTING];
    }

    myMediaPlayerViewController * playerVC = [[myMediaPlayerViewController alloc] initWithMediaDict:infoDict];
    [self.navigationController pushViewController:playerVC animated:ANIMATION_SETTING];
}

回答by talkol

This is the code I'm using to play a file from URL:

这是我用来从 URL 播放文件的代码:

MPMoviePlayerViewController *movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:contentUrl]];
movieViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:movieViewController];
[movieViewController release];

It seems to work fine for me. Two notes:

这对我来说似乎很好。两个注意事项:

  • Some simulators (like the current iOS 5.0) crash when playing a movie, but it works on a real device
  • If you leave out the movieSourceTypepart, a black screen is shown for about a second before the movie starts
  • 一些模拟器(如当前的 iOS 5.0)在播放电影时会崩溃,但它可以在真实设备上运行
  • 如果您省略了该movieSourceType部分,则在电影开始前会显示大约一秒钟的黑屏

回答by plop

you must do like that with CGRectMake(0, 0, 0, 0) in the finished callback!

您必须在完成的回调中使用 CGRectMake(0, 0, 0, 0) 这样做!

-(void)playMovieAtURL:(NSURL*)theURL

{
    MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:theURL];
    theMovie.scalingMode=MPMovieScalingModeAspectFill;

    theMovie.view.frame = CGRectMake(2, 246, 317, 70);
    [self.view addSubview:theMovie.view];

    // Register for the playback finished notification. 

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(myMovieFinishedCallback:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:theMovie];

    // Movie playback is asynchronous, so this method returns immediately. 
    [theMovie play];
} 

// When the movie is done,release the controller. 
-(void)myMovieFinishedCallback:(NSNotification*)aNotification 
{
    MPMoviePlayerController* theMovie=[aNotification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:MPMoviePlayerPlaybackDidFinishNotification 
                                                  object:theMovie];

    theMovie.view.frame = CGRectMake(0, 0, 0, 0);
    // Release the movie instance created in playMovieAtURL
    [theMovie release];

}