AVSpeechSynthesizer in background mode
- You must set "Audio and AirPlay" in background modes.
- 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
- Add on target's info.plist the key "Required background modes" to "App plays audio or streams audio/video using AirPlay".
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
Make Uicollectionview Zoomable
How to Set Http Header Fields in Objective-C
Mfmailcomposeviewcontroller Crashes Because of Global Appearance Properties on iOS6
Fix Cordova Geolocation Ask for Location Message
How to iOS Item and Font Sizes Scaling as Screen Size
Issue with Uitableview: Action Only Works Every Second Time
How to Tell What Profile/Signing Certificate Was Used to Sign .Ipa
How to Prevent Screen Lock on My Application with Swift on iOS
How to Get Notified in Contact Changed Event in iOS
Get Instance of Viewcontroller from Appdelegate in Swift
Exception Type: Exc_Crash (Sigabrt)
Replace a Particular Color Inside an Image with Another Color
How to Send Data from iPhone to Apple Watch in Os2 in Objective-C
How to Install an Unsigned IPA File on My Device Using an Apple Developer Account