尝试在Xcode 7 / Swift 2中播放视频时出错

时间:2015-07-30 15:31:56

标签: ios xcode swift swift2 xcode7

当我输入视频控制器时,我正试图在视频控制器中播放视频,但下面列出了一个错误。对视频播放不太熟悉,所以它应该在视图控制器中,我该如何修复它错误?视频存储在xcode中。感谢。

import UIKit
import MediaPlayer
import AVFoundation

class AuroraViewController: UIViewController {



@IBOutlet var AuroraViewController: UIView!

var moviePlayer: AVPlayer?


private func playVideo() {
    if let path = NSBundle.mainBundle().pathForResource("Aurora", ofType:"mp4") {
        let url = NSURL(fileURLWithPath: path)

下面的行是我得到错误的地方:表达式类型不明确没有更多上下文

        moviePlayer = AVPlayer(contentURL: url) {
            self.moviePlayer = moviePlayer
            moviePlayer.AuroraViewController.frame = self.AuroraViewController.bounds
            moviePlayer.prepareToPlay()
            moviePlayer.scalingMode = .AspectFill
            self.AuroraViewController.addSubview(moviePlayer.view)
        }
    } else {
        debugPrint("Ops, something wrong when playing video.m4v")
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    playVideo()
}




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



}

更新 更正代码:

import UIKit 
import AVFoundation 
import AVKit 

class AuroraViewController: AVPlayerViewController { 
private func playVideo() { 
    if let path = NSBundle.mainBundle().pathForResource("Aurora", ofType: "mp4") { 
        let url = NSURL(fileURLWithPath: path) 
        player = AVPlayer(URL: url) 
    } 
    else { 
        println("Oops, could not find resource Aurora.mp4") 
    } 
} 

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 
    playVideo() 
} 
}

1 个答案:

答案 0 :(得分:1)

这是一次'完全重写'。

以下完成了这项工作:

添加" AVKit.framework"到了#34;构建阶段" - > " Link Binary with Libraries"在项目设置中。

在Storyboard中,将UIViewController的身份更改为AuroraViewController。

然后只需要生成的代码:

import UIKit 
import AVFoundation 
import AVKit 

class AuroraViewController: AVPlayerViewController { 
    private func playVideo() { 
        if let path = NSBundle.mainBundle().pathForResource("Aurora", ofType: "mp4") { 
            let url = NSURL(fileURLWithPath: path) 
            player = AVPlayer(URL: url) 
        } 
        else { 
            println("Oops, could not find resource Aurora.mp4") 
        } 
    } 

    override func viewDidAppear(animated: Bool) { 
        super.viewDidAppear(animated) 
        playVideo() 
    } 
}