用相机录制视频后如何播放?

时间:2018-10-02 06:18:24

标签: swift avfoundation

我正在制作一个摄像头应用程序,我希望能够播放按停止按钮后拍摄的录制的视频。我该怎么做?这是我在这里的代码:

func stopRecording() {

    print("stop the recording")
    guard let writer = assetWriter else { return }

    assetWriterVideoInput = nil
    assetWriterAudioInput = nil
    assetWriterInputPixelBufferAdaptor = nil
    assetWriter = nil

    DispatchQueue.main.async {
        self.delegate?.recorderWillStartWriting()
    }

    capture?.queue.async {
        writer.endSession(atSourceTime: self.currentVideoTime)
        writer.finishWriting {
            switch writer.status {
            case .failed:
                DispatchQueue.main.async {
                    self.delegate?.recorderDidFail(with: RecorderError.couldNotCompleteWritingVideo)
                    print("fail")
                }
            case .completed:
                DispatchQueue.main.async {
                    self.delegate?.recorderDidFinishWriting(outputURL: writer.outputURL)
                    print("completed video")


                }
            default:
                break
            }
        }
        DispatchQueue.main.async {
            self.delegate?.recorderDidFinishRecording()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您在writer.outputURL上有输出视频的URL

要播放视频,只需创建一个AVPlayer

let player = AVPlayer(url: url)

然后创建一个AVPlayerLayer并将其添加到视图中

let playerLayer = AVPlayerLayer(player: player)
view.layer.addSublayer(playerLayer)

或出示AVPlayerViewController

let controller = AVPlayerViewController()
controller.player = player

然后

player.play()

请参见https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing