Flutter语音识别插件google_speech的使用

发布于 1周前 作者 wuwangju 来自 Flutter

Flutter语音识别插件google_speech的使用

Google Speech插件允许在Flutter应用中使用Google Speech API进行语音识别。该插件通过gRPC实现,并支持流式转录功能。以下是详细的使用步骤和示例代码。

准备工作

在开始使用之前,您需要创建一个Google Cloud账户并启用Speech API。具体步骤可以参考官方文档

  1. 创建服务账号并下载JSON格式的凭证文件。
  2. 确保您的项目中已添加了google_speech插件。
dependencies:
  google_speech: ^5.0.0

认证方式

通过服务账号认证

有两种方式可以通过服务账号进行认证:

  1. 直接传入JSON文件

    import 'package:google_speech/speech_client_authenticator.dart';
    import 'dart:io';
    
    final serviceAccount = ServiceAccount.fromFile(File('PATH_TO_FILE'));
    
    final speechToText = SpeechToText.viaServiceAccount(serviceAccount);
    
  2. 传入JSON字符串

    final serviceAccount = ServiceAccount.fromString(r'''{YOUR_JSON_STRING}''');
    
    /// 或者从assets加载数据
    final serviceAccount = ServiceAccount.fromString(
        '${(await rootBundle.loadString('assets/test_service_account.json'))}');
    
    final speechToText = SpeechToText.viaServiceAccount(serviceAccount);
    

通过Token认证(版本 >= 3.0.0)

final speechToText = SpeechToText.viaToken(
    'Bearer',
    '<token-here>',
);

通过第三方认证器认证(版本 >= 3.0.0)

final speechToText = SpeechToText.viaThirdPartyAuthenticator(
    ThirdPartyAuthenticator(
        obtainCredentialsFromThirdParty: () async {
            // 请求API获取token
            final json = await requestCredentialFromMyApi();
            return AccessCredentials.fromJson(json);
        },
    ),
);

转录音频文件

使用recognize方法

  1. 定义RecognitionConfig:

    final config = RecognitionConfig(
        encoding: AudioEncoding.LINEAR16,
        model: RecognitionModel.basic,
        enableAutomaticPunctuation: true,
        sampleRateHertz: 16000,
        languageCode: 'en-US');
    
  2. 获取音频文件内容:

    Future<List<int>> _getAudioContent(String name) async {
        final directory = await getApplicationDocumentsDirectory();
        final path = directory.path + '/$name';
        return File(path).readAsBytesSync().toList();
    }
    
    final audio = await _getAudioContent('test.wav');
    
  3. 发送请求:

    final response = await speechToText.recognize(config, audio);
    

使用streamRecognize方法

  1. 定义StreamingRecognitionConfig:

    final streamingConfig = StreamingRecognitionConfig(config: config, interimResults: true);
    
  2. 获取音频文件流:

    Future<Stream<List<int>>> _getAudioStream(String name) async {
        final directory = await getApplicationDocumentsDirectory();
        final path = directory.path + '/$name';
        return File(path).openRead();
    }
    
    final audio = await _getAudioStream('test.wav');
    
  3. 发送请求:

    final responseStream = speechToText.streamingRecognize(streamingConfig, audio);
    responseStream.listen((data) {
        // 监听响应
    });
    

示例应用

以下是一个完整的Flutter示例应用,演示如何使用google_speech插件进行语音识别:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_speech/google_speech.dart';
import 'package:path_provider/path_provider.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Audio File Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: AudioRecognize(),
    );
  }
}

class AudioRecognize extends StatefulWidget {
  [@override](/user/override)
  _AudioRecognizeState createState() => _AudioRecognizeState();
}

class _AudioRecognizeState extends State<AudioRecognize> {
  bool recognizing = false;
  bool recognizeFinished = false;
  String text = '';

  void recognize() async {
    setState(() {
      recognizing = true;
    });

    final serviceAccount = ServiceAccount.fromString(
        '${(await rootBundle.loadString('assets/test_service_account.json'))}');
    final speechToText = SpeechToText.viaServiceAccount(serviceAccount);
    final config = _getConfig();
    final audio = await _getAudioContent('test.wav');

    await speechToText.recognize(config, audio).then((value) {
      setState(() {
        text = value.results.map((e) => e.alternatives.first.transcript).join('\n');
      });
    }).whenComplete(() => setState(() {
          recognizeFinished = true;
          recognizing = false;
        }));
  }

  void streamingRecognize() async {
    setState(() {
      recognizing = true;
    });

    final serviceAccount = ServiceAccount.fromString(
        '${(await rootBundle.loadString('assets/test_service_account.json'))}');
    final speechToText = SpeechToText.viaServiceAccount(serviceAccount);
    final config = _getConfig();

    final responseStream = speechToText.streamingRecognize(
        StreamingRecognitionConfig(config: config, interimResults: true),
        await _getAudioStream('test.wav'));

    responseStream.listen((data) {
      setState(() {
        text = data.results.map((e) => e.alternatives.first.transcript).join('\n');
        recognizeFinished = true;
      });
    }, onDone: () {
      setState(() {
        recognizing = false;
      });
    });
  }

  RecognitionConfig _getConfig() => RecognitionConfig(
        encoding: AudioEncoding.LINEAR16,
        model: RecognitionModel.basic,
        enableAutomaticPunctuation: true,
        sampleRateHertz: 16000,
        languageCode: 'en-US',
      );

  Future<List<int>> _getAudioContent(String name) async {
    final directory = await getApplicationDocumentsDirectory();
    final path = directory.path + '/$name';
    return File(path).readAsBytesSync().toList();
  }

  Future<Stream<List<int>>> _getAudioStream(String name) async {
    final directory = await getApplicationDocumentsDirectory();
    final path = directory.path + '/$name';
    return File(path).openRead();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Audio File Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            ElevatedButton(
              onPressed: recognize,
              child: Text('Recognize'),
            ),
            ElevatedButton(
              onPressed: streamingRecognize,
              child: Text('Streaming Recognize'),
            ),
            _RecognizeContent(text: text),
          ],
        ),
      ),
    );
  }
}

class _RecognizeContent extends StatelessWidget {
  final String text;

  const _RecognizeContent({Key? key, required this.text}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: [
          Text('Recognized Text:'),
          Text(text),
        ],
      ),
    );
  }
}

更多关于Flutter语音识别插件google_speech的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter语音识别插件google_speech的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用google_speech插件进行语音识别的示例代码。这个插件允许你将音频数据发送到Google的语音识别服务,并获取识别的文本结果。

首先,确保你已经在pubspec.yaml文件中添加了google_speech依赖:

dependencies:
  flutter:
    sdk: flutter
  google_speech: ^x.y.z  # 请替换为最新版本号

然后,运行flutter pub get来安装依赖。

接下来,是一个完整的Flutter应用示例,展示了如何使用google_speech插件进行语音识别:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Google Speech Recognition',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SpeechRecognitionPage(),
    );
  }
}

class SpeechRecognitionPage extends StatefulWidget {
  @override
  _SpeechRecognitionPageState createState() => _SpeechRecognitionPageState();
}

class _SpeechRecognitionPageState extends State<SpeechRecognitionPage> {
  String recognizedText = '';
  bool isListening = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Google Speech Recognition'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Recognized Text:'),
            Text(recognizedText, style: TextStyle(fontSize: 20)),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: isListening ? null : startListening,
              child: Text(isListening ? 'Stop Listening' : 'Start Listening'),
            ),
          ],
        ),
      ),
    );
  }

  void startListening() async {
    setState(() {
      isListening = true;
    });

    try {
      final result = await GoogleSpeech.recognize(
        locale: 'en-US',  // 可根据需要设置语言
        model: 'default', // 可选 'default', 'phone_call', 'video', 'command_and_search'
        alternativeLanguageCodes: [], // 可选,支持的其他语言代码列表
        enableAutomaticPunctuation: true, // 是否自动添加标点符号
        profanityFilter: false, // 是否启用不雅词汇过滤
      );

      setState(() {
        recognizedText = result.text;
        isListening = false;
      });
    } catch (e) {
      print('Error: $e');
      setState(() {
        isListening = false;
      });
    }
  }
}

说明:

  1. 依赖添加:在pubspec.yaml文件中添加google_speech依赖。
  2. UI布局:使用Material Design创建一个简单的UI,包含一个文本显示框和一个按钮。
  3. 语音识别逻辑
    • 当按钮被点击时,如果当前不在监听状态,则开始监听。
    • 调用GoogleSpeech.recognize方法进行语音识别。
    • 识别完成后,更新UI显示识别的文本,并停止监听状态。
    • 捕获并处理任何可能的异常。

注意事项:

  • 权限:确保你的应用有录音权限。在Android上,你需要在AndroidManifest.xml中添加录音权限。
  • 网络:该插件需要将音频数据发送到Google的服务器进行识别,因此你的应用需要网络权限和访问外网的能力。
  • API限制:Google的语音识别API可能有使用限制和配额限制,请根据实际需求进行评估。

希望这个示例能帮助你理解如何在Flutter项目中使用google_speech插件进行语音识别。

回到顶部