0

我使用此功能修剪音頻文件的特定部分。 在函數中,我正在向新文件寫入兩個AVMetadataItems。 我試圖通過用AvURlAsset敲擊URL然後訪問AVAsset metaData/commonMetadata來訪問這些項目,並且都沒有包含我之前添加的項目。如何將AVMetaDateItem寫入文件並稍後訪問?

因此,我檢查了在導出文件之前會發生什麼,即使我給exporter.metadata一個數組哦哦AVMetadataitem,它在出口之前仍然顯示爲零。

任何人都知道是什麼問題?

func trimFromOffSetWithDuration(_ from: Date, startOffSet: TimeInterval, duration: TimeInterval, newfileName: String, file: URL, completion: fileExportaionBlock?) { 

    if let asset = AVURLAsset(url: file) as AVAsset? { 

     let trimmedFileUrl = documentsDirectory().appendingPathComponent(newfileName) 

     let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) 
     exporter?.outputFileType = AVFileTypeAppleM4A 
     exporter?.outputURL = trimmedFileUrl 

     let start = CMTimeMake(Int64(startOffSet), 1) 
     let end = CMTimeMake(Int64(startOffSet + duration), 1) 
     exporter?.timeRange = CMTimeRangeFromTimeToTime(start, end) 

     let typeMetaDataItem = AVMutableMetadataItem() 
     typeMetaDataItem.key = AVMetadataCommonKeyDescription as NSCopying & NSObjectProtocol 
     typeMetaDataItem.keySpace = AVMetadataKeySpaceCommon 
     typeMetaDataItem.value = TrackType.recording.rawValue as NSCopying & NSObjectProtocol 

     let dateMetaDataItem = AVMutableMetadataItem() 
     dateMetaDataItem.key = AVMetadataCommonKeyCreationDate as NSCopying & NSObjectProtocol 
     dateMetaDataItem.keySpace = AVMetadataKeySpaceCommon 
     dateMetaDataItem.value = from as NSCopying & NSObjectProtocol 

     exporter?.metadata? = [dateMetaDataItem, typeMetaDataItem] 
     exporter?.exportAsynchronously { handler in 

      if exporter?.status != AVAssetExportSessionStatus.completed { 
       print("Error while exporting \(exporter?.error?.localizedDescription ?? "unknown")") 
       completion?(nil) 
       return 
      } 
     } 

     completion?(trimmedFileUrl) 
    } 
} 

回答

0

你應該的exportAsynchronously完成塊內移動completion?(trimmedFileUrl)

+0

我明白你的意思,但它仍然沒有工作,這不是問題,謝謝任何方式! –

相關問題