Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
877 stars 573 forks source link

[BUG]:startRecord back home #1082

Closed lqh1008 closed 2 months ago

lqh1008 commented 2 months ago

Flutter Sound Version :


Severity

Platforms you faced the error


Describe the bug start record must back home

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

    this is my code

    
    import 'dart:convert';

import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_sound/flutter_sound.dart'; import 'dart:async'; import 'dart:typed_data'; import 'package:http/http.dart' as http; import 'package:permission_handler/permission_handler.dart';

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

@override _AsrPageState createState() => _AsrPageState(); }

class _AsrPageState extends State { late FlutterSoundRecorder _recorder; bool _isRecording = false; final StreamController _streamController = StreamController();

@override void initState() { super.initState(); _recorder = FlutterSoundRecorder(); _recorder.openRecorder(); }

Future _configureAudioSession() async { const MethodChannel _channel = MethodChannel('com.dooboolab.flutter_sound'); try { await _channel.invokeMethod('configureAudioSession'); } catch (e) { print('Failed to configure audio session: $e'); } }

Future _requestPermissions() async { var status = await Permission.microphone.status; if (!status.isGranted) { status = await Permission.microphone.request(); } if (!status.isGranted) { // 如果用户拒绝了权限请求,可以在此显示提示信息或跳转到设置 print('Microphone permission is not granted'); } }

Future _startRecording() async { await _requestPermissions(); await _configureAudioSession(); try { await _recorder.startRecorder( codec: Codec.pcm16, toStream: _streamController.sink, // 将数据流入 StreamController 的 sink ); setState(() { _isRecording = true; }); } catch (e) { print('Failed to start recording: $e'); } // await _recorder.startRecorder( // codec: Codec.pcm16, // toStream: _streamController.sink, // 将数据流入 StreamController 的 sink // ); // setState(() { // _isRecording = true; // }); }

Future _stopRecording() async { await _recorder.stopRecorder(); setState(() { _isRecording = false; }); }

void _sendToASR(Uint8List buffer) async { // 替换为你的 ASR 接口 URL const url = 'https://your-asr-endpoint.example.com'; final response = await http.post( Uri.parse(url), body: buffer, headers: {'Content-Type': 'audio/wav'}, ); // 处理 ASR 响应 print(response.body); }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Flutter Sound Recorder'), ), body: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ if (_isRecording) ElevatedButton( onPressed: _stopRecording, child: Text('Stop Recording'), ) else ElevatedButton( onPressed: _startRecording, child: Text('Start Recording'), ), StreamBuilder( stream: _streamController.stream, builder: (context, snapshot) { if (snapshot.hasData) { // print('${jsonEncode(snapshot.data)}'); // final Uint8List buffer = snapshot.data!.data!; // // 实时发送数据到 ASR // _sendToASR(buffer); return Text('Recording...'); } else { return Text('Not Recording'); } }, ), ], ), ); }

@override void dispose() { _recorder.stopRecorder(); _recorder.closeRecorder(); _streamController.close(); super.dispose(); } }


Larpoux commented 2 months ago

Hi @lqh1008 , I don’t really understand what is your issue. Don’t forget to send your logs.

Larpoux commented 2 months ago
  Future<void> _configureAudioSession() async {
    const MethodChannel _channel = MethodChannel('com.dooboolab.flutter_sound');
    try {
      await _channel.invokeMethod('configureAudioSession');
    } catch (e) {
      print('Failed to configure audio session: $e');
    }
  }

This is incorrect you may not use the Flutter Sound internal channel. This is internal to flutter sound. If you want to configure your audio-session, you must use an external plugin, like audio_session