2017-10-18 89 views
0

我想在swift 3.0的登錄屏幕上播放背景視頻。但是當我運行程序時,視頻不能像全屏一樣工作。它正在解決這個問題?這是故事板圖片。以全屏方式在登錄頁面上播放視頻

enter image description here

enter code here 
import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

var Player: AVPlayer! 
var PlayerLayer: AVPlayerLayer! 


var frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) 

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


    let URL:NSURL = NSURL(string: "https://firebasestorage.googleapis.com/v0/b/pattyapp-34c16.appspot.com/o/dog1.mp4?alt=media&token=98ab3c41-c645-4535-a70b-41511b5df602")! 


    Player = AVPlayer.init(url: URL as URL) 


    PlayerLayer = AVPlayerLayer(player: Player) 
    PlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect 
    PlayerLayer.frame.size = frame.size 


    Player.actionAtItemEnd = AVPlayerActionAtItemEnd.none 
    Player.isMuted = true 

    Player.play() 

    view.layer.insertSublayer(PlayerLayer, at: 0) 
    NotificationCenter.default.addObserver(self, selector: #selector(playerItemReachEnd(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: Player.currentItem) 
} 
func playerItemReachEnd(notification: NSNotification) { 

    Player.seek(to: kCMTimeZero) 

} 


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

回答

0

視頻,你正試圖播放具有尺寸與層的大小不同。我推薦你試試 PlayerLayer.videoGravity = AVLayerVideoGravityResizePlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill