iOS 16, AVFoundation, Text-to-Speech

Hi, I've been having problems with the AVFoundation and getting text to speech to work with iOS 16.

I have a button which activates the text to speech that says the time. It worked with iOS 15 but not 16. Anyone else having a problem?

Here's the code: (Note: dtd is coming from another Swift file).


import Foundation

import AVFoundation



struct SUINightMode: View {

  

  @ObservedObject var dtd = DateTimeData()

  

  @Environment(\.presentationMode) var presentationMode

  

  @State private var date = Date()

  

    var body: some View {

      

      ZStack {

        

        Color.black

        

          .ignoresSafeArea()

        

        GeometryReader { cen in

        

          VStack (alignment: .center) {

      

          Text("\(dtd.timeString(date: dtd.date))")

            .foregroundColor(.green)

          .font(.system(size: 120, weight: .bold, design: .rounded))

          .alignmentGuide(VerticalAlignment.center, computeValue: { $0[.bottom] })

                              .position(x: cen.size.width / 2, y: cen .size.height / 2)

          HStack(alignment: .center) {

          Text("\(dtd.dayString(date: dtd.date)), ")

            .foregroundColor(.green)

            .font(.system(size: 50, weight: .bold, design: .rounded))

            Text("\(dtd.dateString(date: dtd.date))")

            .foregroundColor(.green)

          .font(.system(size: 50, weight: .bold, design: .rounded))

    }

    }

      Image(systemName: "speaker.wave.3.fill")

          .resizable()

          .foregroundColor(.green)

          .frame(width: 50, height: 30)

          .padding(10)

          .position(x: 120, y: 40)

          .onTapGesture {

            let synthesizer = AVSpeechSynthesizer()

            let speakTime = AVSpeechUtterance(string: "The time is, \(dtd.timeString(date: dtd.date))...., today is \(dtd.dayString(date: dtd.date)) \(dtd.dateString(date: dtd.date))")

            synthesizer.speak(speakTime)

          }

      Image(systemName: "arrow.left")

          .resizable()

          .foregroundColor(.green)

          .frame(width: 45, height: 30)

          .position(x: 40, y: 40)

      .onTapGesture {

        presentationMode.wrappedValue.dismiss()
      }
      .onAppear(perform: {let _ = self.dtd.updateTimer})
      }
    }

      }

}

struct SUINightMode_Previews: PreviewProvider {

    static var previews: some View {
      if #available(iOS 15.0, *) {
        SUINightMode()
          .previewInterfaceOrientation(.landscapeRight)
      } else {
        // Fallback on earlier versions

      }
    }
}

Replies

Yes, I have the same problem. Everything worked with IOS 15, now the same applications do not reproduce speech.

Try declaring

struct SUINightMode: View {
  @ObservedObject var dtd = DateTimeData()
  @Environment(\.presentationMode) var presentationMode
  @State private var date = Date()
  let synthesizer = AVSpeechSynthesizer()    <---------


    var body: some View {
      ZStack {
...
...
...
      Image(systemName: "speaker.wave.3.fill")
          .resizable()
          .foregroundColor(.green)
          .frame(width: 50, height: 30)
          .padding(10)
          .position(x: 120, y: 40)
          
           .onTapGesture {
           let speakTime = AVSpeechUtterance(string: "The time is, \(dtd.timeString(date: dtd.date))...., today is \(dtd.dayString(date: dtd.date)) \(dtd.dateString(date: dtd.date))")
           synthesizer.speak(speakTime)
...
...
           }
        } 
     }
}

It helped me.