When playing a video exported from AVAssetExportSession , you hear the sound long before watching the video. Audio is played immediately, but video only appears after several recording cycles (i.e., it starts and ends). In other words, you hear the sound from the video several times before you see any images.
We use AutoLayout on iOS 8.
Using the following test, we allocated the task to exportAsynchronouslyWithCompletionHandler . In both blocks of code, we play an existing video - not related to export - so the export process was excluded as a variable.
Code 1 plays both video and audio at the beginning, while Code 2 plays only audio at the beginning and shows the video after a delay of 10-60 seconds (several times after video loops).
The only difference between the two code blocks is the use of exportAsynchronouslyWithCompletionHandler to play the video, and the other is not.
Help? Is it possible that the sound is exported first and ready to play before the video? Is something export related happening in another thread?
func initPlayer(videoURL: NSURL) { // Create player player = AVPlayer(URL: videoURL) let playerItem = player.currentItem let asset = playerItem.asset playerLayer = AVPlayerLayer(player: player) playerLayer.frame = videoView.frame view.layer.addSublayer(playerLayer) player.seekToTime(kCMTimeZero) player.actionAtItemEnd = .None player.play() // Get notified when video done for looping purposes NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem) // Log status println("Initialized video player: \(CMTimeGetSeconds(asset.duration)) seconds & \(asset.tracks.count) tracks for \(videoURL)") } func playExistingVideo() { let filename = "/ChopsticksVideo.mp4" let allPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let docsPath = allPaths[0] as! NSString let exportPath = docsPath.stringByAppendingFormat(filename) let exportURL = NSURL.fileURLWithPath(exportPath as String)! initPlayer(exportURL) }
Code 1:
// Create exporter let exporter = AVAssetExportSession(asset: mainComposition, presetName: AVAssetExportPresetHighestQuality) exporter.videoComposition = videoComposition exporter.outputFileType = AVFileTypeMPEG4 exporter.outputURL = exportURL exporter.shouldOptimizeForNetworkUse = true playExistingVideo()
Code 2:
// Create exporter let exporter = AVAssetExportSession(asset: mainComposition, presetName: AVAssetExportPresetHighestQuality) exporter.videoComposition = videoComposition exporter.outputFileType = AVFileTypeMPEG4 exporter.outputURL = exportURL exporter.shouldOptimizeForNetworkUse = true //