in iOS 15, on stopSpeaking of AVSpeechSynthesizer
,
didFinish delegate method getting called instead of didCancel
which is working fine in iOS 14 and below version.
iOS 15 - AVSpeechSynthesizerDelegate didCancel not getting called
Can you file a bug with a sample project and paste the feedback ID?
Hello, I got the same issue. I paste herebelow the code to reproduce. There are 2 files. When run on iOS 15.5, it will print "from inside didFinish speaking". When run on iOS14.3, it will print ""from inside didCancel" which is the correct behaviour according to Apple doc (https://developer.apple.com/documentation/avfaudio/avspeechsynthesizerdelegate/1619678-speechsynthesizer). Done with Xcode 13.4
1st file------------------------
import UIKit
import AVFoundation
class bugReporting: AVSpeechSynthesizer {
static var shared = bugReporting() private override init() { super.init() delegate = self }
let utterance = AVSpeechUtterance(string: "This is to demo the bug of iOS15 not calling didCancel method when stopSpeaking method is called on synthesizer")
func forTrial() { utterance.voice = AVSpeechSynthesisVoice(language: "english") self.speak(utterance)
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
self.stopSpeaking(at: .immediate) } } }
extension bugReporting: AVSpeechSynthesizerDelegate {
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) { print("from inside didCancel") }
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) { print("from inside didFinish speaking") }
}
--------end of 1st file---------
2nd file------------------
import UIKit
class simpleView: UIViewController { override func viewDidLoad() { let speaker = bugReporting.shared speaker.forTrial() } }
-----------end of 2nd file
I am still experiencing this in iOS 18.1 using Xcode 16. Feedback filed: FB15164652
See the following sample project:
import SwiftUI
import AVFoundation
class Synthesizer: NSObject, AVSpeechSynthesizerDelegate {
let avSpeechSynthesizer = AVSpeechSynthesizer()
override init() {
super.init()
avSpeechSynthesizer.delegate = self
}
func play() {
let utterance = AVSpeechUtterance(string: "Hello world, this is a long string of many words many words many words many words many words")
avSpeechSynthesizer.speak(utterance)
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
print("didStart \(utterance)")
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
self?.avSpeechSynthesizer.stopSpeaking(at: .word)
}
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
print("didFinish \(utterance)")
}
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didCancel utterance: AVSpeechUtterance) {
print("didCancel \(utterance)")
}
}
struct ContentView: View {
@State private var synthesizer = Synthesizer()
var body: some View {
Button("Play") {
synthesizer.play()
}
}
}