Swift AVAudioPlayer wont play audio recorded with AVAudioRecorder

后端 未结 2 528
离开以前
离开以前 2021-01-17 00:18

I have built an app that records an audio clip, and saves it to a file called xxx.m4a.

Once the recording is made, I can find this xxx.m4a

2条回答
  •  有刺的猬
    2021-01-17 01:04

    In the place where you want to use the Playback using speakers before you initialize your AVAudioPlayer, add the following code:

    let recordingSession = AVAudioSession.sharedInstance()
            do{
                try recordingSession.setCategory(AVAudioSessionCategoryPlayback)
            }catch{
    
            }
    

    and when you are just recording using AVAudioRecorder use this before initializing that:

    do {
                recordingSession = AVAudioSession.sharedInstance()
                try recordingSession.setCategory(AVAudioSessionCategoryRecord)
        }catch {}
    

提交回复
热议问题