csdcorp / speech_to_text

A Flutter plugin that exposes device specific text to speech recognition capability.
BSD 3-Clause "New" or "Revised" License
351 stars 218 forks source link

Adding sounds for iOS seems partially broken #423

Closed jasonmillen closed 6 months ago

jasonmillen commented 9 months ago

I was testing adding the optional sound asset files to get the plugin to play when it starts/stops listening on iOS. The sound thats played when listening starts (speech_to_text_listening.m4r) seems to work well. But I think something may be broken with the sound thats played when listening is stopped.

I investigated and played around a bit with SwiftSpeechToTextPlugin.swift, and I think what may be happening is that the listening stopped sound is getting cut off? Occasionally when stopping, I hear the slightest bit of the start of the sound file.

I tested putting in a delay after playing the sound and was able to get the sound to fully play. Specifically what I did in testing was change this line to the following:

if let mySound = successSound {
  mySound.play()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
  self.stopSpeech(result)
}

I am testing this w/ our own app and seeing the same behavior on both physical iOS device and simulator.

I don't think we are doing anything unexpected in regards to how we're using the plugin, but I can also try to setup the sample audio_player_interaction app to see if it happens there too. If not, then maybe it is an issue w/ how we're using the plugin.

sowens-csd commented 9 months ago

Where did you make that change? Which method / line? I just had a look at the current code and the end sound should complete before the audio session is closed. If you called stop/cancel again before the sound completed then that could cause it. Do you get the same behaviour in the example app?

jasonmillen commented 9 months ago

I'm observing the same behavior in the example app. When I run the example app and click 'Loop test', I hear the notification.m4r sound play, then directly after I hear the speech_to_text_listening.m4r sound play which indicates the plugin is listening. But I never hear the speech_to_text_stop.m4r sound when listening stops.

Then a new loop will start and I'll hear notification.m4r and speech_to_text_listening.m4r again - and it will keep repeating these sounds in a loop as expected - but it never plays speech_to_text_stop.m4r.

However if I make the change described in my first comment, then I do hear the speech_to_text_stop.m4r sound in the example app. The specific change is to replace line 137 of SwiftSpeechToTextPlugin.swift with the following:

if let mySound = successSound {
  mySound.play()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
  self.stopSpeech(result)
}

Probably unrelated, but it may also be worth noting that in order to get the example app to loop properly, I had to slightly increase the listenFor duration in the _speechToText.listen call from 1 second to 3 seconds. When the duration was 1 second, the loop seemed to get stuck after the first listen (not sure why), so the sounds only played once. After changing to 3 seconds, it would repeat the loop and play the sounds each time as expected.

jasonmillen commented 8 months ago

Just wanted to bump this thread to see if you observe the same behavior I describe above

sowens-csd commented 6 months ago

Yes, I was able to reproduce this and have a fix in the 6.4.0 version that's in the repo now and will release shortly. Thanks for reporting!