How to initialize a AudioPlaybackIntent with parameters?

  Kiến thức lập trình

I am trying to create this

struct PlayAudioIntent: AudioPlaybackIntent {

  private let fileName:String = ""
  private let fileExtension:String = ""

  init() {
    do {
      try AVAudioSession.sharedInstance().setCategory(.playback)
      try AVAudioSession.sharedInstance().setActive(true)
    } catch {
      print("Failed to set up audio session category: (error)")
    }
  }

  init(_ fileName:String, _ fileExtension:String) {
    self.fileName = fileName
    self.fileExtension = fileExtension
  }

  func perform() async throws -> some IntentResult {
    if PlayerModel.shared.isPlaying {
      PlayerModel.shared.pauseAudio()
    } else {
        PlayerModel.shared.setupAudio(fileName, fileExtension)
    }
    return .result()
  }
}

when the code arrives at

PlayerModel.shared.setupAudio(fileName, fileExtension)

fileName and fileExtension are empty.

Why is that if the code is executing the init(filename,fileExtension) correctly in theory saving the variables with the proper values?

LEAVE A COMMENT