ios - View Controller 的对象 swift 隐藏在视频背景后面

标签 ios swift video viewcontroller

我想为我的应用程序创建一个登录页面,在这种情况下,我想使用视频作为登录页面的背景,例如 vine、spotify、uber... 我播放视频没有问题,并在 Storyboard中添加一些按钮和标签到我的 View Controller ,运行应用程序后我看不到 View Controller 的对象 我只是看到视频和隐藏在视频背景后面的对象如何解决这个问题,,, 谢谢你的帮助

这是我播放视频的代码,,,

 lazy var playerLayer:AVPlayerLayer = {

    let player = AVPlayer(URL:  NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("BWWalkthrough", ofType: "mp4")!))
    player.muted = true
    player.allowsExternalPlayback = false
    player.appliesMediaSelectionCriteriaAutomatically = false
    var error:NSError?

    // This is needed so it would not cut off users audio (if listening to music etc.
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    } catch var error1 as NSError {
        error = error1
    } catch {
        fatalError()
    }
    if error != nil {
        print(error)
    }

    var playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.frame
    playerLayer.videoGravity = "AVLayerVideoGravityResizeAspectFill"
    playerLayer.backgroundColor = UIColor.blackColor().CGColor
    player.play()
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"playerDidReachEnd", name:AVPlayerItemDidPlayToEndTimeNotification, object:nil)
    return playerLayer
    }()


    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.view.layer.addSublayer(self.playerLayer)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    playerLayer.frame = self.view.frame
}
func playerDidReachEnd(){
    self.playerLayer.player!.seekToTime(kCMTimeZero)
    self.playerLayer.player!.play()

}

最佳答案

您需要在 Storyboard加载时创建的所有 View 下方添加您的 playerLayer。替换

self.view.layer.addSublayer(self.playerLayer)

self.view.layer.insertSublayer(self.playerLayer, atIndex:0)

应该完成这项工作,除非您确实希望在视频层后面有任何 View Controller View 的 subview 。

关于ios - View Controller 的对象 swift 隐藏在视频背景后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041354/

相关文章:

ios - 在 Core Data 中,如何在 swift 4 中执行 `if exists update else insert`?

ios - 如何让 UICollectionView 在不离开屏幕的情况下使用自动布局填充其包含的 ScrollView 的宽度?

ios - 我们什么时候应该使用 CATransformLayer?

ios - 使用 swift 检查 Firebase 中的用户类型

android - ios 上的 cordova 应用程序中的视频没有声音

php - 修复 javascript 中不正确转义的字符串

ios - UICollectionViewCell 初始化中出现意外错误

iOS - 没有 VOIP/FCM 的 PushKit 和 CallKit

javascript - 如何在 Web 应用程序中嵌入响应式(Reactive) Youtube 播放器(例如 Facebook 上使用的播放器)?

c# - 将当前 WebBrowser DOM 获取为 HTML