Home > AI > IOS > Speech >

Speech Record Button

This is a simple implementation of integrating state machine.


struct SpeechRecordBtnView : View {
    
    @State var state: SpeechState = .notAuth
    @ObservedObject var manager = SpeechAuthManager.shared
    
    
    var backgroundColor: Color {
        switch state {
        case .notAuth:
            return .gray
        case .idle:
            return .accentColor
        case .recording:
            return .red
        case .cancel:
            return .init(white: 0.1)
        }
    }
    
    var scale: CGFloat {
        switch state {
        case .notAuth:
            return 1.0
        case .idle:
            return 1.0
        case .recording:
            return 1.8
        case .cancel:
            return 1.4
        }
    }
    
    public var body: some View {
        
        ZStack {
            backgroundColor
                .animation(.easeOut(duration: 0.2))
                .clipShape(Circle())
                .zIndex(0)
            
            Image(systemName: state != .cancel ? "waveform" : "xmark")
                .font(.system(size: 30, weight: .medium, design: .default))
                .foregroundColor(.white)
                .opacity(state == .recording ? 0.8 : 1.0)
                .padding(20)
                .transition(.opacity)
                .layoutPriority(2)
                .zIndex(1)
            
        }
        .scaleEffect(scale)
        .shadow(color: Color(.sRGBLinear, white: 0, opacity: 0.2), radius: 5, x: 0, y: 3)
           
    }
    
}


enum SpeechState {
    case notAuth
    case idle
    case recording
    case cancel
}



class SpeechAuthManager: ObservableObject {
    // singleton
    static let shared = SpeechAuthManager()
     
     
    // status
    @Published var authStatus: SFSpeechRecognizerAuthorizationStatus = SFSpeechRecognizer.authorizationStatus()
     
     
    // request
    func requestSpeechRecognitionGrant() {
 
        SFSpeechRecognizer.requestAuthorization { authStatus in
            if self.authStatus != authStatus {
                DispatchQueue.main.async {
                    self.authStatus = authStatus
                }
            }
        }
    }
}

Leave a Reply