I’m struggling to get a vibration working from my App.
I have downloaded Asad/Azam Morsecode project https://github.com/AsadAzam/MorseCode
and it works, so my device supports vibrations.
App background: I am using on-device speech recognition to identify words and phrases, I want a correct phrase to trigger a small vibration. In this way, the phone can be in a pocket and a Lavalier Mic can be used to control the app with simple vibration confirmations.
My code looks like this:
import SwiftUI
import CoreHaptics
class cInterpreter : ObservableObject
{
var engine: CHHapticEngine!
init()
{
guard CHHapticEngine.capabilitiesForHardware().supportsHaptics else { return }
do
{
self.engine = try CHHapticEngine()
try self.engine?.start()
}
catch
{
print("There was an error creating the engine: (error.localizedDescription)")
}
}
...
func someFunc(saidString : String)
{
// check if candidate string is recognised
...
// Play a confirmation buzz
if (foundACandidate)
{
// DispatchQueue.global(qos: .utility).async
// {
let vibe = CHHapticEvent(eventType: .hapticContinuous, parameters: [.init(parameterID: .hapticIntensity, value: 0.81), .init(parameterID: .hapticSharpness, value: 0.8)], relativeTime: 0, duration: 0.12)
do
{
let pattern = try CHHapticPattern(events: [vibe], parameters: [])
let player = try self.engine?.makePlayer(with: pattern)
print("Playing...")
try player?.start(atTime: 0)
}
catch
{
print("Failed to play pattern: (error.localizedDescription).")
}
// }
print("Buzz")
}
I get “Playing” and “Buzz” in the console, but no haptic on the phone. Any ideas what I’m doing wrong? Cheers.
Hadders is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.