How to get AVAudioPlayer output to the speaker

前端 未结 10 1679
孤城傲影
孤城傲影 2020-12-07 14:29

I\'m recording audio with AVAudioRecorder as seen in How do I record audio on iPhone with AVAudioRecorder?

I then use AVAudioPlayer to play back the recording. Howev

相关标签:
10条回答
  • 2020-12-07 15:07

    Answer For Swift 4.0

    func SetSessionPlayerOn()
    {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
        } catch _ {
        }
    }
    func SetSessionPlayerOff()
    {
        do {
            try AVAudioSession.sharedInstance().setActive(false)
        } catch _ {
        }
    }
    
    0 讨论(0)
  • 2020-12-07 15:09

    this is the key line to output auido in speaker instead in microphone. Note it should be set while recording

    try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
    

    Below is complete function to setup recording

    private func setupRecorder() {
    
        if isAudioRecordingGranted {
            let session = AVAudioSession.sharedInstance()
            do {
    
                if #available(iOS 10.0, *) {
                    try! session.setCategory(.playAndRecord, mode:.spokenAudio)
                }
                else {
                    // Workaround until https://forums.swift.org/t/using-methods-marked-unavailable-in-swift-4-2/14949 isn't fixed
                   session.perform(NSSelectorFromString("setCategory:error:"), with: AVAudioSession.Category.playback)
                }
                try session.setActive(true)
                try session.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
    
                let settings = [
                    AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                    AVSampleRateKey: 44100,
                    AVNumberOfChannelsKey: 2,
                    AVEncoderAudioQualityKey:AVAudioQuality.high.rawValue
                ]
                audioRecorder = try AVAudioRecorder(url: fileUrl(), settings: settings)
                audioRecorder.delegate = self
                audioRecorder.isMeteringEnabled = true
                audioRecorder.prepareToRecord()
                self.recorderState = .Ready
            }
            catch let error {
                recorderState = .error(error)
            }
        }
        else {
            recorderState = .Failed("Don't have access to use your microphone.")
        }
    }
    
    0 讨论(0)
  • 2020-12-07 15:11

    Swift 3

    Before recording the sound I set:

    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
    

    and before playback I set:

    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    

    Objective C

    before recording:

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryRecord error:nil];
    

    before playback:

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
    
    0 讨论(0)
  • 2020-12-07 15:14

    From http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/12890-audiosessionsetproperty-problem-playing-sound-listening-mic.html --

    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);    
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    
    0 讨论(0)
  • 2020-12-07 15:16

    This works great for me in Swift2

    let session = AVAudioSession.sharedInstance()
    try! session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.DefaultToSpeaker)
    
    0 讨论(0)
  • 2020-12-07 15:21

    I realize this question is fairly old but when I was struggling with the same problem I found a simple solution that hopefully will help anyone else looking to use the louder media speakers as opposed to the receiver speakers. The method I used was setting up the audio session with the DefaultToSpeaker option in AVAudioSessionCategoryOptions:

    In Swift (assuming your audio session is named session) -

    session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions:AVAudioSessionCategoryOptions.DefaultToSpeaker, error: nil)
    

    In Obj-C -

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
    
    0 讨论(0)
提交回复
热议问题