设置视频帧AVPlayer

时间:2016-03-30 13:25:34

标签: ios swift video avplayer

我正在尝试设置AVPlayer的视频帧,使其占据屏幕大小的75%,并使用透明度让背景视图可见。

但是,我无法设置新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个视频:

  • 我认为是视频游戏延伸的那个
  • 具有自定义框架的

我不知道如何在这些比赛中只有一个比赛尺寸合适。

1 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我,但看起来你在不同的视图控制器中使用VideoPlayer类。您应该能够从该VC控制帧,而不是VideoPlayer类。因此,如果您转到包含VideoPlayer的VC,请创建videoPlayer var -

Example: var videoPlayer : VideoPlayer! 

然后在你的viewdidload中你应该可以调用你的函数。通过说

videoPlayer.setUpAndPlayVideo

希望这有帮助。