7

Our app lets users record a video, after which the app adds subtitles and exports the edited video.

The goal is to replay the video immediately, but the AVPlayer only appears after the video finishes (and only plays audio, which is a separate issue).

Here's what happens now: we show a preview so the user can see what he is recording in real-time. After the user is done recording, we want to play back the video for review. Unfortunately, no video appears, and only audio plays back. An image representing some frame of the video appears when the audio is done playing back.

Why is this happening?

func exportDidFinish(exporter: AVAssetExportSession) {
    println("Finished exporting video")

    // Save video to photo album
    let assetLibrary = ALAssetsLibrary()
    assetLibrary.writeVideoAtPathToSavedPhotosAlbum(exporter.outputURL, completionBlock: {(url: NSURL!, error: NSError!) in
        println("Saved video to album \(exporter.outputURL)")
        self.playPreview(exporter.outputURL)
        if (error != nil) {
            println("Error saving video")
        }
    })
}


func playPreview(videoUrl: NSURL) {
    let asset = AVAsset.assetWithURL(videoUrl) as? AVAsset
    let playerItem = AVPlayerItem(asset: asset)
    player = AVPlayer(playerItem: playerItem)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = view.frame
    view.layer.addSublayer(playerLayer)
    player.play()
}
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • It is unclear to me what you are expecting. Please rephrase. Are you trying to show what the camera is recording while the video is being recorded? When you say "the AVPlayer only appears after the video finishes", do you mean the AVPlayer only appears after the recording is finished? – Jörn Eyrich Jun 28 '15 at 15:13
  • @JörnEyrich sorry for the confusion. Here's what happens now: we show a preview so the user can see what he is recording in real-time. After the user is done recording, we want to play back the video. Unfortunately, no video appears, and only audio plays back. An image representing some frame of the video appears when the audio is done playing back. – Crashalot Jun 28 '15 at 20:12
  • Maybe the video you generate is broken? How does it look when you play it from the album you saved it to? Alternatively, if you play some other video in playPreview() instead of the one you just recorded, does that show correctly? – Jörn Eyrich Jun 29 '15 at 21:40
  • @JörnEyrich It looks fine when played from the album. Will check on other videos and report back. – Crashalot Jun 29 '15 at 21:47

2 Answers2

3

Perhaps this can help:

let assetLibrary = ALAssetsLibrary()
assetLibrary.writeVideoAtPathToSavedPhotosAlbum(exporter.outputURL, completionBlock: {(url: NSURL!, error: NSError!) in
     if (error != nil) {
       println("Error saving video")
     }else{
       println("Saved video to album \(url)")
       self.playPreview(url)
     }
})

Send "url" to "playPreview" leaving "completionBlock" and not that which comes from "AVAssetExportSession"

Perhaps...!

eliasRuizHz
  • 356
  • 4
  • 15
2

The answer was we had an incorrectly composed video in the first place, as described here: AVAssetExportSession export fails non-deterministically with error: "Operation Stopped, NSLocalizedFailureReason=The video could not be composed.".

The other part of the question (audio playing long before images/video appears) was answered here: Long delay before seeing video when AVPlayer created in exportAsynchronouslyWithCompletionHandler

Hope these help someone avoid the suffering we endured! :)

Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439