iOS Code to Convert m4a to WAV

后端 未结 6 666
小鲜肉
小鲜肉 2020-12-14 13:48

Does anyone have any code snippets that show how to convert an M4a file to WAV? I know there are libraries that convert the other way around.

Thanks.

6条回答
  •  情歌与酒
    2020-12-14 14:18

    I simply changed the extension of the file to .wav and removed the .m4a file and it worked.

    func getDirectory() -> URL {
        let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let documentDirectory = path[0]
        return documentDirectory
    }
    
    let date = Date().timeIntervalSince1970
    
    fileName = getDirectory().appendingPathComponent("\(date).m4a")
    wavFileName = getDirectory().appendingPathComponent("\(date).wav")
    
    try! FileManager.default.copyItem(at: fileName, to: wavFileName)
    try! FileManager.default.removeItem(at: fileName)
    

    I even played .wav file and it's working fine.

    audioPlayer = try! AVAudioPlayer(contentsOf: wavFileName)
    audioPlayer.play()
    

    Are there any drawbacks for converting the file extension from .m4a to .wav like this?

提交回复
热议问题