改编 Krishna 的答案,用于AVAssetExportSession
将 保存MPMediaItem
到文件中,您可以在 Swift 3 中执行以下操作:
/// Export MPMediaItem to temporary file.
///
/// - Parameters:
/// - assetURL: The `assetURL` of the `MPMediaItem`.
/// - completionHandler: Closure to be called when the export is done. The parameters are a boolean `success`, the `URL` of the temporary file, and an optional `Error` if there was any problem. The parameters of the closure are:
///
/// - fileURL: The `URL` of the temporary file created for the exported results.
/// - error: The `Error`, if any, of the asynchronous export process.
func export(_ assetURL: URL, completionHandler: @escaping (_ fileURL: URL?, _ error: Error?) -> ()) {
let asset = AVURLAsset(url: assetURL)
guard let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
completionHandler(nil, ExportError.unableToCreateExporter)
return
}
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(NSUUID().uuidString)
.appendingPathExtension("m4a")
exporter.outputURL = fileURL
exporter.outputFileType = "com.apple.m4a-audio"
exporter.exportAsynchronously {
if exporter.status == .completed {
completionHandler(fileURL, nil)
} else {
completionHandler(nil, exporter.error)
}
}
}
func exampleUsage(with mediaItem: MPMediaItem) {
if let assetURL = mediaItem.assetURL {
export(assetURL) { fileURL, error in
guard let fileURL = fileURL, error == nil else {
print("export failed: \(error)")
return
}
// use fileURL of temporary file here
print("\(fileURL)")
}
}
}
enum ExportError: Error {
case unableToCreateExporter
}
或者,在 Swift 2 中:
/// Export MPMediaItem to temporary file.
///
/// - Parameters:
/// - assetURL: The `assetURL` of the `MPMediaItem`.
/// - completionHandler: Closure to be called when the export is done. The parameters are a boolean `success`, the `URL` of the temporary file, and an optional `Error` if there was any problem. The parameters of the closure are:
///
/// - fileURL: The `URL` of the temporary file created for the exported results.
/// - error: The `Error`, if any, of the asynchronous export process.
func export(assetURL: NSURL, completionHandler: (NSURL?, ErrorType?) -> ()) {
let asset = AVURLAsset(URL: assetURL)
guard let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
completionHandler(nil, ExportError.unableToCreateExporter)
return
}
let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory())
.URLByAppendingPathComponent(NSUUID().UUIDString)!
.URLByAppendingPathExtension("m4a")
exporter.outputURL = fileURL
exporter.outputFileType = "com.apple.m4a-audio"
exporter.exportAsynchronouslyWithCompletionHandler {
if exporter.status == .Completed {
completionHandler(fileURL, nil)
} else {
completionHandler(nil, exporter.error)
}
}
}
func exampleUsage(with mediaItem: MPMediaItem) {
if let assetURL = mediaItem.assetURL {
export(assetURL) { fileURL, error in
guard let fileURL = fileURL where error == nil else {
print("export failed: \(error)")
return
}
// use fileURL of temporary file here
print("\(fileURL)")
}
}
}
enum ExportError: ErrorType {
case unableToCreateExporter
}
如您所见,我将它放在一个临时文件夹而不是 Documents 文件夹中。此外,我使用 UUID 而不是自某个参考日期以来的秒数来生成临时文件。但思路基本相同。