Issue playing slow-mo AVAsset in AVPlayer

后端 未结 3 1295
滥情空心
滥情空心 2020-12-18 09:09

I\'m trying to play an slow motion video (filmed by the user\'s iPhone) in an AVPlayer.

I am retrieving the AVAsset with a request on a

相关标签:
3条回答
  • 2020-12-18 09:36

    For those coming here looking for a swift answer, this is the swift translation, which I use in my project, where I need the url of the slow-motion Video to play it with the AVPlayerViewController:

    else if asset is AVComposition {
    //Slow-Motion Assets are passed as AVComposition
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory: NSString? = paths.first as NSString?
    if documentsDirectory != nil {
        let random = Int(arc4random() % 1000)
        let pathToAppend = String(format: "mergeSlowMoVideo-%d.mov", random)
        let myPathDocs = documentsDirectory!.strings(byAppendingPaths: [pathToAppend])
        let myPath = myPathDocs.first
        if myPath != nil {
            let url = URL(fileURLWithPath: myPath!)
            let exporter = AVAssetExportSession(asset: asset!, presetName: AVAssetExportPresetHighestQuality)
            if exporter != nil {
            exporter!.outputURL = url
            exporter!.outputFileType = AVFileTypeQuickTimeMovie
            exporter!.shouldOptimizeForNetworkUse = true
            exporter!.exportAsynchronously(completionHandler: {
                AsyncUtil.asyncMain {
                    let url = exporter!.outputURL
                    if url != nil {
                        let player = AVPlayer(url: url!)
                        let playerViewController = AVPlayerViewController()
                        playerViewController.player = player
                        playerViewController.modalTransitionStyle = .crossDissolve
                        view.present(playerViewController, animated: true) {
                            playerViewController.player!.play()
                        }
                    }
                }
            })
        }
    }
    
    0 讨论(0)
  • 2020-12-18 09:38

    Playing Slo-mo & Library Video in Swift 4 n above with Custom View

    var vcPlayer = AVPlayerViewController()
    var player = AVPlayer()
    
    func playallVideo(_ customView: UIView, asset: PHAsset) {
            guard asset.mediaType == .video
                else {
                    print("Not a valid video media type")
                    return
            }
            let options = PHVideoRequestOptions()
            options.isNetworkAccessAllowed = true
            PHCachingImageManager().requestPlayerItem(forVideo: asset, options: options) { (playerItem, info) in
                DispatchQueue.main.async {
                    self.player = AVPlayer(playerItem: playerItem)
                    self.vcPlayer.player = self.player
                    self.vcPlayer.view.frame = customView.bounds
                    self.vcPlayer.videoGravity = .resizeAspectFill
                    self.vcPlayer.showsPlaybackControls = true
                    //self.vcPlayer.allowsPictureInPicturePlayback = true
                    self.playerView.addSubview(self.vcPlayer.view)
                    self.player.play()
                }
            }
        }
    
    /**********Function Call ********/
    
    self.playallVideo(self.playerView/*YourCustomView*/, asset: currentAssetArr[currentIndex]/*Current PHAsset Fetched from Library*/)
    

    :) enjoy

    0 讨论(0)
  • 2020-12-18 09:50

    So it turns out that slow motion videos are passed as AVComposition.

    You can export that into a video file / URL, and then handle it like any other video.

    Solution here: https://overflow.buffer.com/2016/02/29/slow-motion-video-ios/

    //Output URL
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = paths.firstObject;
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"mergeSlowMoVideo-%d.mov",arc4random() % 1000]];
    NSURL *url = [NSURL fileURLWithPath:myPathDocs];
    
    //Begin slow mo video export
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
    exporter.outputURL = url;
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    exporter.shouldOptimizeForNetworkUse = YES;
    
    [exporter exportAsynchronouslyWithCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (exporter.status == AVAssetExportSessionStatusCompleted) {
                NSURL *URL = exporter.outputURL;
                NSData *videoData = [NSData dataWithContentsOfURL:URL];
    
                 // Upload
                 [self uploadSelectedVideo:video data:videoData];
             }
        });
    }];
    
    0 讨论(0)
提交回复
热议问题