voice_rsa 0.0.1 copy "voice_rsa: ^0.0.1" to clipboard
voice_rsa: ^0.0.1 copied to clipboard

A Flutter plugin for voice recognition.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:voice_rsa/voice_rsa.dart';
import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _voiceRsaPlugin = VoiceRsa();

  @override
  void initState() {
    super.initState();
    saveFileFromAssetsToCache('assets/test.pcm');

  }

  Future<void> saveFileFromAssetsToCache(String assetFileName) async {
    // Read file from assets
    ByteData data = await rootBundle.load(assetFileName);
    List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    // Get cache directory
    Directory cacheDir = await getTemporaryDirectory();
    String cacheFilePath = '${cacheDir.path}/test.pcm';
    // Write file to cache directory
    var file = await File(cacheFilePath).writeAsBytes(bytes);
    print(file);
    print('File saved to cache: $cacheFilePath');

  }
  Future<void> requestPermission() async {
    if (await Permission.microphone.request().isGranted) {
      // Either the permission was already granted before or the user just granted it.
      print('Microphone permission granted');
    } else {
      print('Microphone permission not granted');
    }
  }
  void onTest() async{
    await requestPermission();
    try {
      Directory cacheDir = await getTemporaryDirectory();
      String cacheFilePath = '${cacheDir.path}/test.pcm';
      // var result =
      //     await _voiceRsaPlugin.getPlatformVersion();
      var result =
      await _voiceRsaPlugin.getVoiceAsr(cacheFilePath,params: {
        'appid': 'baiduAppId',
        'key': 'baiduKey',
        'secret': 'baiduSecret',
        'infile':cacheFilePath,
      });
      print(result);
    } catch(e) {
      print(e);
    }
  }


  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion = '';
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      var isAuthorized = await _voiceRsaPlugin.getSpeechRecognitionAuthorized();
      print(isAuthorized);
      if(isAuthorized==false){
        var result = await _voiceRsaPlugin.requestAuthorization();
        print(result);
      }

      // var result =
      //     await _voiceRsaPlugin.getVoiceAsr(audioPath);
      // if(result != null){
      //   platformVersion = result['message']??'';
      // }
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Center(
              child: Text('Running on: $_platformVersion\n'),
            ),
            TextButton(onPressed: onTest, child: const Text('测试点击'))
          ],
        ),
      ),
    );
  }
}
2
likes
0
points
12
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for voice recognition.

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on voice_rsa

Packages that implement voice_rsa