【问题标题】:MPMoviePlayerController not behaving properlyMPMoviePlayerController 行为不正常
【发布时间】:2016-05-03 07:49:34
【问题描述】:

我正在使用 MPMoviePlayerController 播放一个小视频作为我的应用的介绍视频。我已经使用了以下代码,并且效果很好。但是视频一直在播放。结束并再次重新启动。即使我单击完成按钮,它也不会删除超级视图。我还希望我的状态栏可见,并且我保持半透明仍然没有结果。我在谷歌上搜索和堆栈溢出但仍然没有帮助。任何帮助都会非常显着。

我在viewDidAppear中调用了[self playMovie]方法

-(void)playMovie
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"avc_intro" ofType:@"mp4"]];
moviePlayer =  [[MPMoviePlayerController alloc]
                initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayer];
         name:MPMoviePlayerWillExitFullscreenNotification
                                          object:moviePlayer];

moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *videoplayer = [notification object];
[[NSNotificationCenter defaultCenter]
 removeObserver:self
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:videoplayer];

if ([videoplayer
     respondsToSelector:@selector(setFullscreen:animated:)])
{
    [videoplayer.view removeFromSuperview];
}
}

【问题讨论】:

    标签: ios objective-c xcode mpmovieplayercontroller


    【解决方案1】:

    更好地使用this. MPMoviePlayerController 在 iOS 9 中被正式弃用。

    【讨论】:

      【解决方案2】:

      viewDidload 调用[self playMovie],因为删除MPMoviePlayerController 时再次调用viewDidAppear,因此它将再次启动。

      更新:

      您应该呈现MPMusicPlayerController,而不是将其视图添加到超级视图并在完成时关闭它

      希望这会有所帮助:)

      【讨论】:

      • 如果我在 viewDidLoad 中调用它就不会播放
      • 感谢好友 :) @Lion
      【解决方案3】:

      使用 AVPlayerViewController 我的代码如下,

      objective-C 代码: 声明AVPlayerViewController *playerViewController;

          playerViewController = [[AVPlayerViewController alloc]init];
          playerViewController.view.frame = videoView.bounds;
          [playerViewController.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
          playerViewController.player = [AVPlayer playerWithURL:[NSURL URLWithString:viewModel.videoUrl]];
          [videoView addSubview:playerViewController.view];
          [playerViewController.player play];
          playerViewController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
      

      不要忘记下面给出的框架,

      #import <UIKit/UIKit.h>
      #import <AVFoundation/AVFoundation.h>
      #import <AVKit/AVKit.h>
      #import <MediaPlayer/MediaPlayer.h>
      

      快速代码:

              let type : String! = "mp4"
              let targetURL : String? = NSBundle.mainBundle().pathForResource("Official Apple MacBook Air Video   YouTube", ofType: "mp4")
      
              let videoURL = NSURL(fileURLWithPath:targetURL!)
      
      
              let player = AVPlayer(URL: videoURL)
              let playerController = AVPlayerViewController()
      
              playerController.player = player
              self.addChildViewController(playerController)
              self.playView.addSubview(playerController.view)
              playerController.view.frame = playView.bounds
      
              player.play()
      

      不要忘记下面给出的框架,

      import UIKit
      import AVKit
      import AVFoundation
      import MediaPlayer
      

      它对我有用,希望它有帮助

      【讨论】:

        猜你喜欢
        • 2013-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2015-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多