【发布时间】:2019-08-25 04:08:04
【问题描述】:
我在 iOS 中使用 Swift 使用 cmusphinx,cmusphinx 使用 wav 格式的音频文件,而我需要使用的文件是 mp3 格式。
如何在 iOS 中使用 Swift 将 mp3 格式的音频文件转换为 wav 格式?
到目前为止,我有以下代码使用 Apple AVFoundation 框架中的 AVAssetExportSession,但我一直收到错误消息,说我使用了无效的输出文件类型,无论我设置 AVAssetExportSession 对象的 outputFileType 属性是什么。
我愿意使用 Apple 以外的其他框架。
错误发生在我用“此处错误”的注释标记的地方,其中设置了 AVAssetExportSession.outputFileType 实例属性。
override func viewDidLoad() {
super.viewDidLoad()
let assetURL: URL = Bundle.main.url(forResource: "audio16000", withExtension: "mp3")!
let asset = AVAsset(url: assetURL)
let presets: [String] = AVAssetExportSession.allExportPresets()
for preset in presets {
print(preset)
}
let localDocumentsURL: URL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let outputURL: URL = localDocumentsURL.appendingPathComponent("audio16000").appendingPathExtension("wav")
if FileManager.default.fileExists(atPath: outputURL.path) {
do {
try FileManager.default.removeItem(at: outputURL)
} catch {
print(error)
}
}
let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHEVC1920x1080)
exporter?.outputURL = outputURL
exporter?.outputFileType = AVFileType.wav // error here
exporter?.exportAsynchronously {
print("exporter status =", exporter?.status as Any)
switch exporter!.status {
case .unknown:
print("status unknown")
case .waiting:
print("status waiting")
case .exporting:
print("status exporting")
case .completed:
print("status completed")
case .failed:
print("status failed")
case .cancelled:
print("status cancelled")
@unknown default:
print("@unknown default:")
}
}
}
调试窗口显示如下:
2019-09-17 11:42:14.766703-0500 TrialAVAssetExportSession[5620:2016827] [Accessibility] ****************** Loading GAX Client Bundle ****************
AVAssetExportPreset1920x1080
AVAssetExportPresetLowQuality
AVAssetExportPresetAppleM4A
AVAssetExportPresetHEVCHighestQuality
AVAssetExportPreset640x480
AVAssetExportPreset3840x2160
AVAssetExportPresetHEVC3840x2160
AVAssetExportPresetHighestQuality
AVAssetExportPresetMediumQuality
AVAssetExportPreset1280x720
AVAssetExportPreset960x540
AVAssetExportPresetHEVC1920x1080
2019-09-17 11:42:25.574394-0500 TrialAVAssetExportSession[5620:2016827] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetExportSession setOutputFileType:] Invalid output file type'
*** First throw call stack:
(0x233d4a98c 0x232f239f8 0x239d527f4 0x1004d2f58 0x1004d40f0 0x2601fe224 0x2601fe628 0x2607dce64 0x2607dd40c 0x253f82620 0x2607edce8 0x2607a0908 0x2607a5fe0 0x2600692a4 0x26007183c 0x260068f28 0x260069818 0x260067b64 0x26006782c 0x26006c36c 0x26006d150 0x26006c224 0x260070f24 0x2607a45e8 0x2603a0e04 0x2366c69fc 0x2366d040c 0x2366cfc14 0x100584c78 0x100588840 0x236701040 0x236700cdc 0x236701294 0x233cdc728 0x233cdc6a8 0x233cdbf90 0x233cd6ecc 0x233cd67c0 0x235ed779c 0x2607a7c38 0x1004d5594 0x23379a8e0)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我已经尝试了从 AVAssetExportSession.exportPresets(compatibleWith:) 方法收到的所有预设。当我使用 AVAssetExportSession.allExportPresets() 时,我收到了完全相同的字符串数组。
【问题讨论】:
标签: ios swift mp3 wav cmusphinx