azure_speech_recognition_nic 0.0.4 copy "azure_speech_recognition_nic: ^0.0.4" to clipboard
azure_speech_recognition_nic: ^0.0.4 copied to clipboard

This package allows you to quickly set up Microsoft Azure Translator and get startedwith translation.

example/lib/main.dart

import 'dart:io';
import 'dart:typed_data';

import 'package:azure_speech_recognition_nic/azure_speech_recognition_nic.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

import 'audio_input.dart';

void main() async {
  await dotenv.load(fileName: ".env");
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // int _counter = 0;
  String? _translatedText;
  final AudioInput audioInput = AudioInput();
  bool isRecording = false;

  @override
  void initState() {
    super.initState();
    debugPrint('[xxx] initState()');
    audioInput.init();
    if (dotenv.env['MICROSOFT_AZURE_SUBSCRIPTION_KEY'] != null && dotenv.env['MICROSOFT_AZURE_SUBSCRIPTION_REGION'] != null) {
      AzureSpeechRecognition.initialize(dotenv.env['MICROSOFT_AZURE_SUBSCRIPTION_KEY']!, dotenv.env['MICROSOFT_AZURE_SUBSCRIPTION_REGION']!);
      // translate();
    }
  }

  Future<void> translate(String? path) async {
    if (path == null) {
      debugPrint("[xxx]path is null");
      return;
    }
    File file = File(path);
    debugPrint('[xxx]translate is called...');
    // Uint8List audioBytes = await rootBundle.load('assets/audio/test.wav');
    // Uint8List audioBytes = await rootBundle.load('assets/audio/test1.wav').then((value) => value.buffer.asUint8List());
    Uint8List audioBytes = await file.readAsBytes();
    _translatedText = await AzureSpeechRecognition.instance.recognizeAudio(audioBytes);

    if (_translatedText != null) {
      debugPrint('[xxx]received translation from package..');
      debugPrint('[xxx]translated: $_translatedText');
    }
    setState(() {});
  }

  // 開始錄音
  Future<void> startRecording() async {
    try {
      await audioInput.startRecording();
      setState(() {
        isRecording = true;
      });
    } catch (err) {
      debugPrint("[xxx]_startRecording() error: ${err.toString()}");
    }
  }

  // 停止錄音
  Future<void> stopRecording() async {
    try {
      final path = await audioInput.stopRecording();
      setState(() {
        isRecording = false;
      });
      if (path != null) {
        debugPrint("[xxx]path: $path");
        translate(path);
      }
    } catch (err) {
      debugPrint("_stopRecording() error: ${err.toString()}");
    }
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("[xxx]build() isRecording: $isRecording");
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: isRecording ? ()=>stopRecording():()=>startRecording(),
        child: Icon(isRecording ? Icons.mic_off:Icons.mic),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'translated Text:',
            ),
            Text(
              _translatedText ?? '',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
0
likes
120
points
38
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

This package allows you to quickly set up Microsoft Azure Translator and get startedwith translation.

License

unknown (license)

Dependencies

flutter, http

More

Packages that depend on azure_speech_recognition_nic