我正在使用設備麥克風錄製音頻,其中AVAudioRecorder
返回的文件格式爲.caf只能在Apple設備中播放,但不能在Android設備上播放。由於Apple不支持.mp3文件,所以我想在將文件上傳到服務器之前將其轉換爲.mp4格式。 是.mp4僅適用於音頻嗎?我可以使用AVAssetExportSession
進行轉換嗎?如何將.caf音頻文件轉換爲swmp中的.mp4文件
以下是錄音機代碼:
func setupAudioRecorder()
{
let fileMgr = FileManager.default
let dirPaths = fileMgr.urls(for:.documentDirectory,
in:.userDomainMask)
let soundFileURL = dirPaths[0].appendingPathComponent("myaudio.caf")
let recordSettings =
[AVEncoderAudioQualityKey: AVAudioQuality.min.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100.0] as [String : Any]
do {
try audioSession.setCategory(
AVAudioSessionCategoryPlayAndRecord)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
do {
try audioRecorder = AVAudioRecorder(url: soundFileURL,
settings: recordSettings as [String : AnyObject])
audioRecorder?.prepareToRecord()
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
}
感謝您的後幫助,但我也需要.caf,因爲在上傳到服務器之前,我需要用AVAudioPlayer播放音頻。 –
您可以使用AVAudioPlayer播放AAC。 –
好的,讓我試試這個設置 –