我試圖設置AVPlayer的視頻幀,使其佔用屏幕大小的75%,並讓背景視圖使用透明度可見。設置視頻幀AVPlayer
但是,我無法設置我的新VC的大小,它總是需要整個寬度。
這裏是我的代碼:
import AVKit
import AVFoundation
class VideoPlayer {
// ----------------------------------------------------------------------------------
// MARK: - Properties
// ----------------------------------------------------------------------------------
var videoName = "here video url"
let playerViewController = AVPlayerViewController()
// ----------------------------------------------------------------------------------
// MARK: - Setup
// ----------------------------------------------------------------------------------
func setupAndPlayVideo(sourceViewController: UIViewController) {
// Get video URL
let videoURL = urlForFile(videoName)
let player = AVPlayer(URL: videoURL!)
// Grey transparent background
playerViewController.view.backgroundColor = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 0.5)
// Prevent user from pausing/dismissign the video
playerViewController.showsPlaybackControls = false
// Present the video player VC and play video
playerViewController.player = player
// This does not change anything (sample hard coded values)
playerViewController.view.frame = CGRectMake(0, 0, 300, 300)
sourceViewController.presentViewController(playerViewController, animated: true) {
self.playerViewController.player!.play()
}
}
}
編輯
所以我決定改變我的策略,並在我的故事板添加AVPlayerViewController。
這裏是我用來播放我的視頻代碼:
// Game over
case PapooSegue.GameEndSegue.rawValue:
// Get photo VC
let videoVC = segue.destinationViewController as! AVPlayerViewController
// Get video URL and provide it to the player
let videoName = papooBrain!.getGameOverVideoName()
let videoURL = urlForFile(videoName)
videoVC.player = AVPlayer(URL: videoURL!)
// Set grey/transparent background
videoVC.view.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.6)
// Play video
videoVC.player?.play()
的問題仍然是相同的。我試圖與AVPlayerLayer as suggested in this post玩,但我結束了2個視頻播放:
- 由videoGravity拉伸我猜
- 的一個具有自定義框架的一個
我不我不知道如何才能在適當的尺寸下打出一個。
對不起,您正在使用avplayerviewocntroller默認值。所以在這種情況下,你應該使用childviewcontroller來達到你想要的。 –
恐怕我沒有得到你的建議:/ – H4Hugo
嗯,讓我嘗試在這種情況下的任何事情:D。對不起,因爲我的錯誤回答。 –