在视图控制器中播放视频

时间:2016-04-21 08:19:52

标签: ios swift

仅供参考,我正在使用这个Github播放器:https://github.com/piemonte/Player

我正在尝试播放视频,但它无法正常播放。在View Controller的UIView上,我使用背景颜色为蓝色,当我运行此项目时,我只看到没有播放器的bue背景。这是我的代码:

import UIKit
import Player
import AVFoundation

class ViewController: UIViewController, PlayerDelegate {

    @IBOutlet var myView: UIView!

    private var player: Player!

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

        let fileUrl = NSURL(string: "https://parseapi.back4app.com/files/SAQY2fBCiob9UMqITqAEMo7UxNsU6MCCUURf53Ee/d349a0bdd0962cb0f570c95621aee6df_video.mp4")

        self.player = Player()
        self.player.delegate = self
        player.view.frame.size.width = UIScreen.mainScreen().bounds.size.width
        player.view.frame.size.height = player.view.frame.size.width

        player.fillMode = AVLayerVideoGravityResizeAspectFill
        player.view.sizeToFit()
        myView.addSubview(player.view)

        let videoUrl: NSURL = fileUrl!
        self.player.setUrl(videoUrl)
        self.player.playFromBeginning()

        self.player.playbackLoops = true

        let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
        tapGestureRecognizer.numberOfTapsRequired = 1
        self.player.view.addGestureRecognizer(tapGestureRecognizer)
    }

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

    // MARK: PlayerDelegate Start

    func handleTapGestureRecognizer(gestureRecognizer: UITapGestureRecognizer) {
        switch (self.player.playbackState.rawValue) {
        case PlaybackState.Stopped.rawValue:
            self.player.playFromBeginning()
        case PlaybackState.Paused.rawValue:
            self.player.playFromCurrentTime()
        case PlaybackState.Playing.rawValue:
            self.player.pause()
        case PlaybackState.Failed.rawValue:
            self.player.pause()
        default:
            self.player.pause()
        }
    }

    func playerReady(player: Player) {
    }

    func playerPlaybackStateDidChange(player: Player) {
    }

    func playerBufferingStateDidChange(player: Player) {
    }

    func playerPlaybackWillStartFromBeginning(player: Player) {
    }

    func playerPlaybackDidEnd(player: Player) {
    }

    /* override func awakeFromNib() {

     self.collectionView.autoresizesSubviews = true
     self.collectionView.autoresizingMask = UIViewAutoresizing.FlexibleWidth
     self.collectionView.translatesAutoresizingMaskIntoConstraints = true
     }*/

    // MARK: PlayerDelegate Stop
}

尝试nr 2:

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

        let fileUrl = NSURL(string: "https://parseapi.back4app.com/files/SAQY2fBCiob9UMqITqAEMo7UxNsU6MCCUURf53Ee/d349a0bdd0962cb0f570c95621aee6df_video.mp4")

        self.player = Player()
        self.player.delegate = self
        self.player.view.frame = self.view.bounds

        self.addChildViewController(self.player)
        self.view.addSubview(self.player.view)
        self.player.didMoveToParentViewController(self)

        let videoUrl: NSURL = fileUrl!
        self.player.setUrl(videoUrl)
        self.player.playFromBeginning()

        self.player.playbackLoops = true

        let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
        tapGestureRecognizer.numberOfTapsRequired = 1
        self.player.view.addGestureRecognizer(tapGestureRecognizer)
    }

当我尝试在Safari中打开链接时,它无法正常工作,但在Chrome中它可以正常工作..有什么建议可能有什么问题吗?

1 个答案:

答案 0 :(得分:0)

实际上播放器实际上还没有添加到你的视图中。

请按照此操作将其添加到您的视图中作为子视图控制器。

 self.player = Player()
 self.player.delegate = self
 self.player.view.frame = self.view.bounds

 self.addChildViewController(self.player)
 self.view.addSubview(self.player.view)
 self.player.didMoveToParentViewController(self)