视频播放GPUImage和swift

时间:2015-10-20 19:34:09

标签: ios objective-c swift ios9 gpuimage

我想用chromakey过滤器处理视频,输出应该在屏幕上重现。

在我开发的代码下面没有显示任何视频,我无法理解原因:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let aSelector : Selector = "start:"
    let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
    tapGesture.numberOfTapsRequired = 1
    view.addGestureRecognizer(tapGesture)
}

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

@IBAction func start(sender: AnyObject) {
    NSLog("tap pressed")
    play()
}

func play(){
    let path = NSBundle.mainBundle().pathForResource("glass_buster", ofType: "mp4")
    let player = AVPlayer()
    let pathURL = NSURL.fileURLWithPath(path!)
    NSLog(pathURL.absoluteString)
    let playerItem = AVPlayerItem(URL: pathURL)
    player.replaceCurrentItemWithPlayerItem(playerItem)


    let gpuMovie = GPUImageMovie(playerItem: playerItem)
    gpuMovie.playAtActualSpeed = true
    let filter = GPUImageChromaKeyFilter()
    gpuMovie.addTarget(filter)
    gpuMovie.playAtActualSpeed = true


    gpuMovie.startProcessing()
    player.play()


}

1 个答案:

答案 0 :(得分:4)

您还需要一个显示电影的GPUImageView,并且您无法将播放器存储在本地var中。请尝试以下方法:

var player: AVPlayer! = nil
var playerItem : AVPlayerItem! = nil

func play() {
    let path = NSBundle.mainBundle().pathForResource("glass_buster", ofType: "mp4")
    player = AVPlayer()
    let pathURL = NSURL.fileURLWithPath(path!)

    playerItem = AVPlayerItem(URL: pathURL)
    player.replaceCurrentItemWithPlayerItem(playerItem)


    let gpuMovie = GPUImageMovie(playerItem: playerItem)
    gpuMovie.playAtActualSpeed = true

    let filteredView: GPUImageView = GPUImageView();
    filteredView.frame = self.view.frame
    self.view.addSubview(filteredView)

    let filter = GPUImageChromaKeyFilter()
    gpuMovie.addTarget(filter)
    gpuMovie.playAtActualSpeed = true
    filter.addTarget(filteredView)

    gpuMovie.startProcessing()
    player.play()
}