我使用Swift在iOS中使用cmusphinx,cmusphinx使用wav格式的音频文件,而我需要使用的文件是mp3格式。
如何使用Swift将mp3格式的音频文件转换为iOS中的wav格式?
到目前为止,我使用来自Apple框架的AVAssetExportSession编写了以下代码,但是我一直收到一个错误,说我使用了无效的输出文件类型,不管我设置了AVAssetExportSession对象的outputFileType属性。
我愿意使用苹果以外的其他框架。
错误发生在我用一个注释标记它的地方,它写着"error here",其中设置了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()时,我会收到相同的字符串数组。
发布于 2019-09-24 07:11:51
https://stackoverflow.com/questions/57643169
复制相似问题