MPMoviePlayerController 在 BACK 按钮上给我一个黑色的空视图

MPMoviePlayerController gives me a black empty view on BACK button

我已经使用 MPMoviePlayerController 在我的网络视图中实时播放视频 url 但是当我弹出视图控制器时,黑屏出现在视图中并且导航栏标题发生了应有的变化。

- (void)viewDidLoad
{
    [super viewDidLoad];



    NSURL *movieURL = [NSURL URLWithString:self.url];
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    self.moviePlayer.contentURL = movieURL;

    if (self.moviePlayer)
    {
        [self.moviePlayer prepareToPlay];

        self.moviePlayer.view.frame = self.view.bounds;

        [self.view addSubview:self.moviePlayer.view];
        [self.moviePlayer.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

        // save the movie player object
        [self.moviePlayer setFullscreen:YES];
        [self.moviePlayer setShouldAutoplay:YES];
        [self.moviePlayer setAllowsAirPlay:YES];

        // Play the movie!
        [self.moviePlayer play];

        [self initializeNavigationController];


    }


}

-(void)initializeNavigationController
{

    UIBarButtonItem *buttonBack = [[UIBarButtonItem alloc] backButtonWithtarget:self action:@selector(Back)];

    self.navigationItem.leftBarButtonItem = buttonBack;
}

-(void)Back
{

    [self.moviePlayer.view removeFromSuperview];
    [self stopLoadingVideoInView];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

现场视频画面

按下返回按钮后

我认为您只需删除 [self.view removeFromSuperview]; 并替换为 [self.moviePlayer.view removeFromSuperview];

如果您将 youMovieController 添加为父视图的子视图,请使用此方法将其删除。

[self.view removeFromSuperView] 

如果您通过 UINavigationController 推送 viewController,只需调用它返回即可。

[self.navigationCotroller popViewController:animated] 

如果您在场 ViewController,请使用它来删除它。

[self dismissViewController] 

终于解决了

在 app delgate 中,我创建了一个方法来再次为 vc 分配内存

- (void)resetAppToFirstController
{
    UIViewController *obj = [[MainMenuViewController alloc] init];


    //Create Navigation Controller to Hold View Controller
    UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:obj];
   [self.window setRootViewController:navigationController];
}

以及我的后退按钮操作

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.resetAppToFirstController;

这解决了我的问题,它可能对 MPMoviePlayer 的某些人有所帮助

移除[self.view removeFromSuperview];并替换为 [self.moviePlayer.view removeFromSuperview];

- (void)resetAppToFirstController
{
    UIViewController *obj = [[MainMenuViewController alloc] init];


    //Create Navigation Controller to Hold View Controller
    UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:obj];
   [self.window setRootViewController:navigationController];
}