Avspeechsynthesizer in Background Mode

AVSpeechSynthesizer in background mode

  1. You must set "Audio and AirPlay" in background modes.
  2. You have to configure the audio session:
    NSError *error = NULL;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
if(error) {
// Do some error handling
}
[session setActive:YES error:&error];
if (error) {
// Do some error handling
}

Use AVSpeechSynthesizer in background without stopping f.e. music app

I found the answer myself...

The important part ist to configure the AVAudioSession with the .duckOthers option:

let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)

This will make playback of f.e. music less loud but this would make it stay less loud even when speech is done. This is why you need to set a delegate for the AVSpeechSynthesizer and then handle it like so:

func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
guard !synthesizer.isSpeaking else { return }

let audioSession = AVAudioSession.sharedInstance()
try? audioSession.setActive(false)
}

That way, music will continue with normal volume after speech is done.

Also, right before speaking, I activate my audioSession just to make sure (not sure if that would really be necessary, but since I do so, I have no more problems...)

Using AVSpeechSynthesizer on background

  1. Add on target's info.plist the key "Required background modes" to "App plays audio or streams audio/video using AirPlay".
  2. Configure the audio session in your AppDelegate:

    NSError *error = NULL;
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:&error];
    if(error) {
    // Do some error handling
    }
    [session setActive:YES error:&error];
    if (error) {
    // Do some error handling
    }

    Don't forget to import AVFoundation.h

Credits to @nicu

AVSpeechSynthesizer stops working after backgrounding

I spent entire day chasing this insanity and I think I have found solution. My issue was that AVSpeechSynthesizer would work fine in foreground and background with ducking other audio right until the moment a phone call happens.

At that moment, speaking would stop working silently, without any errors. All the objects are still there but delegate calls would not get called, neither start nor finish.

I noticed that with phone calls, my app would get notification about AudioRouteChanged. Thus when that happens, I would re-create the speech setup: basically destroy existing AVSpeechSynthesizer and re-create it again. From then on, speaking will continue working. It would even work during the phone call :)

Use `AVSpeechSynthesizer` with `MPRemoteCommandCenter`

i have the same issue, and now figure out
the only thing you need to do

try? AVAudioSession.sharedInstance().setCategory(.playback)

at the begining. don't use .duckOthers, .mixWithOthers

addTarget to RemoteCommand

func addRemoteCommandCenter() {
let rcc = MPRemoteCommandCenter.shared()
//添加暂停监听
rcc.pauseCommand.addTarget(self, action: #selector(playOrPauseEvent(_:)))
//添加播放监听
rcc.playCommand.addTarget(self, action: #selector(playOrPauseEvent(_:)))
//下一首
rcc.nextTrackCommand.addTarget(self, action: #selector(nextCommandEvent(_:)))
//上一首
rcc.previousTrackCommand.addTarget(self, action: #selector(previousCammndEvent(_:)))
//耳机暂停和播放的监听
rcc.togglePlayPauseCommand.addTarget(self, action: #selector(togglePlayPauseCommand(_:)))
}

at the speechSynthesizer(_:didStart:) and speechSynthesizer(_:willSpeakRangeOfSpeechString:utterance:) update the UI

        let infoCenter = MPNowPlayingInfoCenter.default()        
infoCenter.nowPlayingInfo = [MPMediaItemPropertyTitle:"Title", MPMediaItemPropertyArtist: "Artist", MPMediaItemPropertyAlbumTitle: "", MPMediaItemPropertyArtwork: MPMediaItemArtwork(boundsSize: CGSize(width: 120, height: 120), requestHandler: { (size) -> UIImage in
UIImage()
})]


Related Topics



Leave a reply



Submit