Greg
Greg

Reputation: 10788

How can I access voice data from INSendMessageIntent?

I'm trying to add Siri support to my app for sending voice messages. I've implemented INSendMessageIntentHandling in my main app target.

It looks like it's getting as far as recording the voice message and passing my intent handler an INSendMessageIntent with an audio attachment, but I'm not able to read the attachment file.

func handle(
  intent: INSendMessageIntent,
  completion: @escaping (INSendMessageIntentResponse) -> Void
) {
  if let attachment = intent.attachments?.first,
     let audioFile = attachment.audioMessageFile,
     let fileURL = audioFile.fileURL
  {
    // This branch runs
    // fileURL is "file:///var/mobile/tmp/SiriMessages/89F738F7-6092-439A-B4FA-2DD9A99F0EED.caf"
    let result = processMessageAudio(url: fileURL)
    completion(result)
    return
  }

  // This line isn't reached
  completion(.init(code: .failure, userActivity: nil))
}

private func processMessageAudio(url: URL) -> INSendMessageIntentResponse {
  var fileRef: ExtAudioFileRef?
  if url.startAccessingSecurityScopedResource() {
    logDebug("File access allowed")
  } else {
    // This branch runs
    logDebug("File access not allowed")
  }
  defer {
    url.stopAccessingSecurityScopedResource()
  }

  let openStatus = ExtAudioFileOpenURL(url as CFURL, &fileRef)
  // openStatus is -54 (kAudio_FilePermissionError)

  return INSendMessageIntentResponse(code: .failure, userActivity: nil)
}

I'm not sure what I'm missing. It looks like there should be an audio file, and Siri shows a preview of the audio for confirmation.

Upvotes: 0

Views: 21

Answers (0)

Related Questions