在应用委托中调用 MPMoviePlayerViewController

Call MPMoviePlayerViewController in app delegate

我在应用程序中使用故事板。 使用情节提要所有视图都正确连接 现在我想做的新事情是,

当我的启动画面关闭时,我想在用户每次打开应用程序时显示 3 秒的视频。

我知道如何从 viewcontroller 加载视频, 以下是我用来启动视频的代码。

- (void)showVideo
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSLog(@"video path :- %@",url);
    videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    videoController.moviePlayer.controlStyle = MPMovieControlStyleNone;
    [self presentMoviePlayerViewControllerAnimated:videoController];
    [videoController.moviePlayer play];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:videoController];
}

- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];

    [videoController.view removeFromSuperview];
    [videoController.moviePlayer stop];
    videoController = nil;
    [self.view removeFromSuperview];
}

但是当我在我的 rootViewController 应用程序中使用此代码时崩溃并提示

Attempt to present <MPMoviePlayerViewController:> on <DashbaordVC:> whose view is not in the window hierarchy

但是当我在其他使用导航控制器(无情节提要)的演示应用程序中使用相同的代码时,它工作正常。 但是在这个使用故事板的应用程序中,它崩溃了。

我也试过了

[self.navigationController pushViewController:self.videoController animated:NO];

然后我想到在 AppDelegate 文件中添加这段代码并从 ApplicationDidFinishLaunching

调用方法

但没有帮助。 谁能指导我....同样 还有如何在应用程序委托中添加 MPMoviePlayerViewController

我想你可以通过 MPMoviePlayerController 来做到这一点。试试下面的代码

         NSString *path = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
         NSURL *url = [NSURL fileURLWithPath:path];

        videoPlayer = [[MPMoviePlayerController alloc] init];
        [videoPlayer.view setFrame:CGRectMake(0.0, viewTopbar.frame.size.height,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - (viewTopbar.frame.size.height + 50.0))];
                [videoPlayer setMovieSourceType:MPMovieSourceTypeFile];

        [videoPlayer setContentURL:url];
        [videoPlayer setControlStyle:MPMovieControlStyleEmbedded];
        [videoPlayer setScalingMode:MPMovieScalingModeNone];
        [videoPlayer prepareToPlay];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:videoPlayer];

       [self.view addSubview:videoPlayer.view];
       [videoPlayer play];

如果你不想全屏,那么就这样使用它

[videoPlayer setControlStyle:MPMovieControlStyleNone];