How to make AVPlayer appear instantly, and not after the end of the video?

Our application allows users to record video, after which the application adds subtitles and exports the edited video.

The goal is to play the video right away, but AVPlayer appears only after the video is completed (and only audio is played, which is a separate issue).

Here's what's happening now: we are showing a preview so that the user can see what he is recording in real time. After the user has completed the recording, we want to play the video for viewing. Unfortunately, the video does not appear, and only sound is played. An image showing a frame of a video appears when sound is being played.

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() } 
+6
source share
2 answers

The answer was that we had a wrongly composed video in the first place, as described here: Exporting an AVAssetExportSession does not deterministically fail: "Operation stopped, NSLocalizedFailureReason = The video may not be composed." .

Another part of the question (playing sound long before images / videos appeared) answered here: Delay before watching a video when AVPlayer is created in exportAsynchronouslyWithCompletionHandler

Hope this helps someone avoid the misery we have experienced! :)

+1
source

Perhaps this may 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 ", not the one that comes from " AVAssetExportSession "

May be...!

+3
source

Source: https://habr.com/ru/post/989777/


All Articles