My app involves both SwiftSpeech's features and sound effects through SwiftySound. All sound effects work fine in the simulator, but on the device all sound effects stop working once the SwiftSpeech button is pressed. I have one button that makes a "click" noise when pressed. It works until I press the SwiftSpeech button.
If I press the SwiftSpeech button first, I get a sound effect the first time, but then not for subsequent presses.
I made a new, simple project without SwiftSpeech just to test the sound, and everything worked fine on the device. I also switched out SwiftySound and used the normal AVAudioPlayer procedure, and the sound works that way, too.
So the only thing I can think of is that there is a conflict between SwiftSpeech and the sound effects. Is it possible that SwiftSpeech is turning off my sound effects? If so, how do I turn them back on? My code appears below:
let emojiArray = ["๐ต","๐ฆ","๐ถ","๐บ","๐ฆ","๐ฆ","๐ฑ","๐ฆ","๐
","๐ด","๐ฆ","๐ฆ","๐ฎ","๐ท","๐","๐ช","๐ฆ","๐ฆ","๐","๐ฆ","๐ฆ","๐","๐","๐ฐ","๐ฆ","๐ป","๐จ","๐ผ","๐ฆ","๐ฆ","๐","๐ง","๐ฆ
","๐ฆ","๐ฆข","๐ฆ","๐ฆ","๐ธ","๐","๐ข","๐ฆ","๐","๐ณ","๐ฌ","๐","๐","๐","๐ฆ","๐","๐","๐","๐ฆ","๐ท","๐ฆ","๐ฆ"]
@State private var emoji = ""
@State private var nextEmoji = ""
@State private var text = "What is this? (Press and Hold)"
@State private var theDescription = ""
@State var isCorrect:Bool
@State var player = AVAudioPlayer()
var body: some View {
ZStack(alignment:.top) {
VStack(alignment: .center) {
Text (emoji).font(.system(size: 200, weight: .bold, design: .default))
.onAppear() {
emoji = emojiArray.randomElement() ?? "none"
theDescription = emoji.applyingTransform(.toUnicodeName, reverse: false) ?? "None"
print (theDescription) // get the emoji's unicode name
}
Text (text)
.onAppear {
SwiftSpeech.requestSpeechRecognitionAuthorization()
}
.padding()
SwiftSpeech.RecordButton()
}
.swiftSpeechRecordOnHold()
.onRecognize { _, result in
text = result.bestTranscription.formattedString
print (text)
self.text = text
if theDescription.contains(self.text.uppercased()) == true {
print ("That's right")
text = "That's right!"
isCorrect = true
playRightSound()
}
else {print ("That's wrong")
text = "Try again!"
isCorrect = false
playWrongSound()
}
} handleError: { _, _ in }
Spacer()
Button("Change Animal") {
nextEmoji = emojiArray.randomElement() ?? "none"
while nextEmoji == emoji {
nextEmoji = emojiArray.randomElement() ?? "none"
}
playClickSound()
emoji = nextEmoji
text = "What is this? (Press and hold)"
theDescription = emoji.applyingTransform(.toUnicodeName, reverse: false) ?? "None"
print (theDescription)
}
}
}
func playRightSound(){
print ("Playing right sound")
Sound.play(file:"yay.wav")
}
func playWrongSound() {
Sound.play(file:"raspberry.wav")
}
func playClickSound() {
Sound.play(file:"click.wav")
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(isCorrect:true)
}