Playing slow motion videos from camera make it loses slow motion effect

So, I've never faced a problem like this for so long. I have basically scrutinized every possible website on the internet looking for a solution but have found nothing so far.


I have a custom picker controller in which I can select videos and play them. Beforehand, I was struggling to play slow motion videos (only no-slow-motion videos were playing) but after searching I found the solution here.

http://stackoverflow.com/questions/26152396/how-to-access-nsdata-nsurl-of-slow-motion-videos-using-photokit

So, my code to get videos became this:

let videoOptions = PHVideoRequestOptions()
videoOptions.version = PHVideoRequestOptionsVersion.Original
PHImageManager.defaultManager().requestAVAssetForVideo(asset!, options: videoOptions , resultHandler: { (asset, audioMix, info) -> Void in
         if let asset = asset as? AVURLAsset {
              let videoData = NSData(contentsOfURL: asset.URL)
              let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV"
              let videoURL = NSURL(fileURLWithPath: videoPath)
              let writeResult = videoData?.writeToURL(videoURL, atomically: true)
                    
              if let writeResult = writeResult where writeResult {
                      print("success")
                      let videoVC = VideoViewController(videoUrl: videoURL)
                      imagePicker.presentViewController(videoVC, animated: false, completion: nil)
              }
              else {
                    print("failure")
              }
         }
})

Now, slow motion videos are playing but in a normal way instead of in a slow-motion way. This questions relates to the problem.

https://devforums.apple.com/message/903937#903937

I've seen a lot of comments saying they solved the problem by using Photos framework, but I have no idea how to achieve this and they didn't explain either. It might be something to do PHAssetMediaSubtypeVideoHighFrameRate. So, how would be possible to play slow motion videos? Do I need to change the fps somehow?

Please help, I am quite desperate :/ Objective-C code is welcome as well.

Replies

"Slow Motion Playback" is a function of the player, not the video file itself. You will have to tell your player to play at a slow motion rate. Does that make sense?

michaleg, I don't see any properties that cn do " tell your player to play at a slow motion rate". Can you point me in some direction here ?

is anyone found any solution i do mostly on many sites but unable to find any solution

One of the solution that i found was to export video as slow mo in local path and then use it anywhere you want.

PHImageManager.default().requestAVAsset(forVideo: asset, options: options) 
        { (avAsset, audioMix, info) in
            //
//            print(info)
            if let avAssetObj = avAsset as? AVURLAsset
            {
                let filePathUrl = avAssetObj.url
                completionHandler!(true, filePathUrl, nil)
            }
            else if let value = avAsset
            {   // FOR SLOW MOTION VIDEOS
                //avasset returns avcompostion value in case of slo-mo videos
                // Export video to some local path
                let exporter = AVAssetExportSession(asset: value, presetName: AVAssetExportPresetHighestQuality)
                let randNum:Int = Int(arc4random())
                
                //Generating Export Path
                let exportPath: NSString = NSTemporaryDirectory().appendingFormat("\(randNum)"+"video.mov") as NSString
                let exportUrl: NSURL = NSURL.fileURL(withPath: exportPath as String) as NSURL
                //SettingUp Export Path as URL
                exporter?.outputURL = exportUrl as URL
                exporter?.outputFileType = AVFileType.mov
                exporter?.shouldOptimizeForNetworkUse = true
                exporter?.exportAsynchronously(completionHandler: {() -> Void in
                    print("exporter?.progress inside asyc \(exporter?.progress))")
                    DispatchQueue.main.async(execute: {() -> Void in
                        if exporter?.status == .completed, let fileurl = exporter?.outputURL {
                            
                            completionHandler!(true, fileurl , nil)
                        }
                        else if exporter?.status == .failed{
                           // error handling
                        }
                    })
                })
            }
            else {
                // error handling
            }
        }