2016-05-01 67 views
3

我試圖在AVPlayer中播放慢動作視頻(由用戶的iPhone拍攝)。在AVPlayer中播放慢動作AVAsset的問題

我從一選擇器上PHAsset請求檢索AVAsset

[manager requestAVAssetForVideo:PHAsset 
          options:videoRequestOptions 
        resultHandler:^(AVAsset * avasset, AVAudioMix * audioMix, NSDictionary * info) {}]; 

的問題是,一旦發揮,我得到這個錯誤:

-[AVComposition URL]: unrecognized selector sent to instance 0x138d17f40 

但是,如果我設置這個選項對管理員請求,它將以正常的速度播放120/240fps的視頻並且不會崩潰:

videoRequestOptions.version = PHVideoRequestOptionsVersionOriginal; 

怎麼回事?默認version屬性是PHVideoRequestOptionsVersionCurrent其中包含慢動作,用戶編輯和修剪等。

我想播放該視頻版本。謝謝

+0

你能解決這個問題嗎?我正在錄製慢動作視頻,而不是從圖書館中選取它。如果我將它保存在圖書館中,那麼它就可以和慢鏡頭一起工作。如果我在我的應用程序的AVPLayer中查看它,慢動作不起作用。 – Nil

回答

5

所以事實證明,慢動作視頻傳遞爲AVComposition

您可以將其導出到視頻文件/ URL中,然後像處理其他任何視頻一樣處理它。

這裏的解決方案: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

謝謝你的解決方案,但是我還有一些額外的問題。我使用上面的代碼在AVPlayerViewController中播放視頻。但是,當我調用「player.currentItem.currentTime.value」時,它的行爲很奇怪。我花了足夠的時間知道,如果我從照片應用進入視頻,並製作整個視頻慢動作或正常動態,我可以獲得穩定的行爲。這只是標準的iPhone默認設置(在慢動作中,視頻沒有顯示的開始和結束處設置一小段),奇怪的行爲開始了。任何想法如何將這隻保存爲慢動作? – Ibdakine

+0

這是從照片庫中挑選的情況。如果我錄製慢鏡頭視頻然後想要上傳呢? https://stackoverflow.com/questions/47514282/ios-avplayer-cant-play-240-fps-video – Nil

0

對於那些來這裏尋找一個快速的答案,這是迅速的翻譯,我在我的項目中使用,在那裏我需要的慢動作視頻的網址玩它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() 
        } 
       } 
      } 
     }) 
    } 
}