向后播放视频

时间:2017-12-03 09:36:17

标签: ios swift url video avasset

我想要的基本上是反转AVAsset和输出视频文件,我找到了这个链接 -  https://github.com/tempire/ReverseAVAsset/blob/master/AVAsset.swift

这就是我所做的:

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {

    var playerViewController = AVPlayerViewController()
    var playerView = AVPlayer()


    override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)

         let path = Bundle.main.path(forResource: "video", ofType: "mov")
         let outputUrl = URL(fileURLWithPath: "\(NSUUID().uuidString)video.mov")

         let originalAsset = AVAsset(url: URL(fileURLWithPath: path!))
         let reversedAsset = originalAsset.reversedAsset(outputUrl)



         playerView = AVPlayer(playerItem: AVPlayerItem(asset: reversedAsset!))
         playerViewController.player = playerView

         present(playerViewController, animated: true) { 
             self.playerViewController.player?.play()
         }

    }


}

但它不起作用,这就是我最终的结果:

enter image description here

我认为我在使用outputURL做错了,如果你知道该怎么做请帮忙

1 个答案:

答案 0 :(得分:1)

问题在于输出网址,这是正确的方法

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {

    var playerViewController = AVPlayerViewController()
    var playerView = AVPlayer()


    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let path = Bundle.main.path(forResource: "video", ofType: "mov")

        //Correct way
        var outputURL: URL?
        do {
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            outputURL = documentsURL.appendingPathComponent("\(NSUUID().uuidString)newvideo.mp4")

        }

        let originalAsset = AVAsset(url: URL(fileURLWithPath: path!))
        let reversedAsset = originalAsset.reversedAsset(outputUrl)



        playerView = AVPlayer(playerItem: AVPlayerItem(asset: reversedAsset!))
        playerViewController.player = playerView

        present(playerViewController, animated: true) { 
           self.playerViewController.player?.play()
        }

    }


}