【问题标题】:Video not playing on splash screen视频未在启动画面上播放
【发布时间】:2016-05-30 12:06:46
【问题描述】:

我正在尝试显示带有视频的启动画面。为此,我有两个查询:

  1. 我创建了启动画面视图控制器并在应用程序启动时加载它。但是在我的初始屏幕 VC 显示任何修复之前,白屏会显示几秒钟?
  2. 我正在播放的视频仅显示黑屏。我猜不出我做错了什么。下面是它的代码。

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    
    let vc = SplashScreenVC(nibName: "SplashScreenVC", bundle: nil)
    let rootViewController = UINavigationController(rootViewController: vc)
    self.window?.rootViewController = rootViewController
    self.navigationController?.navigationBarHidden = true
    self.window?.makeKeyAndVisible()
    
    // Override point for customization after application launch.
    return true
    

    }

    class SplashScreenVC: UIViewController {
    
    override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBarHidden = true
        playVideo()
    }
    
    func playVideo() {
    
      let moviePath = NSBundle.mainBundle().pathForResource("animationVideo", ofType: "mp4")
      let movieUrl = NSURL.fileURLWithPath(moviePath!)
      let moviePlayer : MPMoviePlayerController = MPMoviePlayerController(contentURL: movieUrl)
      moviePlayer.prepareToPlay()
      let vV = self.view.viewWithTag(SplashScreenUITags.videoView.rawValue)! as UIView
      moviePlayer.controlStyle = MPMovieControlStyle.None
      moviePlayer.scalingMode = MPMovieScalingMode.Fill
      moviePlayer.movieSourceType  = MPMovieSourceType.File;
      moviePlayer.view.frame = vV.bounds
    
      vV.addSubview(moviePlayer.view)
      moviePlayer.play()
    
      NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("moviePlayBackDidFinish:"), name: MPMoviePlayerPlaybackDidFinishNotification, object: moviePlayer)
    
    }
    }
    

【问题讨论】:

    标签: ios iphone mpmovieplayercontroller splash-screen


    【解决方案1】:

    您的一个解决方案是显示 Splash (IMG),然后播放您喜欢的视频。

    要快速播放视频,请使用 AV Foundation https://developer.apple.com/av-foundation/

    您无法摆脱静态飞溅图像。在显示时, 操作系统正在加载应用程序并实例化内容,直到它 准备好调用你的 UIApplicationDelegate。所以你所能做的就是要么 不使用飞溅(黑屏几秒钟)或制作电影 完全从显示的初始屏幕开始,所以它看起来像静态的 图像会突然动画。

    要在电影加载时摆脱黑屏,您可以尝试 使播放器透明并在播放器后面有一个 UIImageView 显示启动图像。行为将是这样的:

    显示启动画面(静态图像)。应用程序已加载。你看 UIImageView,也显示启动画面。最重要的是 透明电影播放器​​。电影播放器​​终于加载了移动和 开始播放。至少在理论上,这应该会导致效果 静态图像突然开始动画。

    但是,如果您根本不使用启动画面(很多游戏都这样做), 那么电影播放器​​显示黑屏并不重要 一开始你不会注意到的。

    关于在 UIImageView 中显示启动画面:不幸的是, 您必须测试界面旋转并手动加载图像, 无法查询显示了哪个启动画面。如果你只 支持一种界面方向(同样,很多游戏都这样做)你 当然没有这个问题。

    【讨论】:

      【解决方案2】:

      关于第二个问题,您可以在 SplashScreenVC 中使用 AVPlayer。很快就会是这样的:

      var player: AVPlayer?
      
      override func viewDidLoad() {
         super.viewDidLoad()
      
         loadVideo()
      }
      
      private func loadVideo() {
      
          //this line is important to prevent background music stop
          do {
              try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
          } catch { }
      
          let path = NSBundle.mainBundle().pathForResource("your path", ofType:"mp4")
      
          player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
          let playerLayer = AVPlayerLayer(player: player)
      
          playerLayer.frame = self.view.frame
          playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
          playerLayer.zPosition = -1
      
          self.view.layer.addSublayer(playerLayer)
      
          player?.seekToTime(kCMTimeZero)
          player?.play()
      }
      

      【讨论】:

        【解决方案3】:

        首先确保在 Launch Screen File Main.storyboard 的 General 选项卡中被选中,因为您使用 ViewController 作为启动视图。

        【讨论】:

          【解决方案4】:

          无法在启动屏幕中播放视频。您要做的就是将视频的第一帧作为启动图像或放入启动屏幕故事板中。编写单独的视图控制器来播放您的实际视频并从情节提要中加载。将此故事板设置为 Xcode 项目的常规选项卡中的主界面。

          【讨论】:

          • 我正在使用闪屏视图控制器来显示视频。在 appDelegate 我是 splashScreenVC 作为 rootView 控制器
          • 好的。你在哪里调用 playVideo()?
          • 我正在更新问题。请看
          猜你喜欢
          • 1970-01-01
          • 2023-03-17
          • 1970-01-01
          • 1970-01-01
          • 2021-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多