【发布时间】:2017-08-29 10:25:07
【问题描述】:
我创建了这个函数来修剪音频文件的特定部分。 在函数中,我将两个 AVMetadataItems 写入新文件。 我稍后尝试通过使用 AvURlAsset 敲击 URL 然后访问 AVAsset 元数据 /commonMetadata 来访问这些项目,但两者都不包含我之前添加的项目。
所以我在导出文件之前检查了发生了什么,即使我给了 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)
}
}
【问题讨论】:
标签: ios swift3 avfoundation avassetexportsession avmetadataitem