我希望状态栏在全屏视频播放期间可见

时间:2018-05-29 15:59:44

标签: ios swift avplayerviewcontroller

我想知道要编写什么代码,以便在全屏模式下播放视频时显示顶部的状态栏。我已经尝试了所有想到的东西但是当视频开始播放时状态栏仍然隐藏起来。

这是当前的viewcontroller代码:

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {
    var playerController = AVPlayerViewController()
    var player:AVPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        let  videoString:String? = Bundle.main.path(forResource: "Buspro", ofType: ".mp4")

        if let url = videoString {
            let videoURL = NSURL(fileURLWithPath: url)

            self.player = AVPlayer(url: videoURL as URL)
            self.playerController.player = self.player
        }
    }

    @IBAction func Play(_ sender: Any) {
        self.playerController.showsPlaybackControls = false
        self.present(self.playerController, animated: true, completion: {
            self.playerController.player?.play()
        })
    }
}

1 个答案:

答案 0 :(得分:0)

AVPlayerViewController是UIViewController只是子类并覆盖prefersStatusBarHidden

创建新文件CustomAVPlayerViewController.Swift

import UIKit
import AVKit

 class CustomAVPlayerViewController: AVPlayerViewController {            
            override var prefersStatusBarHidden: Bool {
                    return false
            }
        }

然后在您的代码中

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {
    var playerController = CustomAVPlayerViewController()
    var player:AVPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        let  videoString:String? = Bundle.main.path(forResource: "Buspro", ofType: ".mp4")

        if let url = videoString {
            let videoURL = NSURL(fileURLWithPath: url)

            self.player = AVPlayer(url: videoURL as URL)
            self.playerController.player = self.player
        }
    }

    @IBAction func Play(_ sender: Any) {
        self.playerController.showsPlaybackControls = false
        self.present(self.playerController, animated: true, completion: {
            self.playerController.player?.play()
        })
    }
}