recorder_handler.dart 3.96 KB
import 'package:appframe/services/dispatcher.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

/*录音相关全局参数*/
FlutterSoundRecorder? localRecorder;
bool isLocalRecording = false;
bool isLocalPauseRecording = false;
String? localRecordedFilePath;

Future<bool> initLocalRecorder() async {
  // 请求麦克风权限
  var status = await Permission.microphone.request();
  if (status != PermissionStatus.granted) {
    throw RecordingPermissionException('麦克风权限未授权!');
  }

  if (!(!isLocalRecording && !isLocalPauseRecording && localRecordedFilePath == null)) {
    return false;
  }

  // 打开录音器
  FlutterSoundRecorder recorder = FlutterSoundRecorder();
  try {
    localRecorder = await recorder.openRecorder();
    return true;
  } catch (e) {
    print(e);
    await _resetRecorderStatus();
    throw Exception('打开录音器失败!');
  }
}

Future<bool> startLocalRecording() async {
  if (isLocalRecording) {
    return false;
  }

  if (!await initLocalRecorder()) {
    return false;
  }

  try {
    // 获取应用文档目录路径
    final directory = await getApplicationDocumentsDirectory();
    String filePath = '${directory.path}/bxe_recording.aac';

    await localRecorder!.startRecorder(toFile: filePath, codec: Codec.aacMP4);

    isLocalRecording = true;
    isLocalPauseRecording = false;
    localRecordedFilePath = filePath;

    return true;
  } catch (e) {
    print(e);
    await _resetRecorderStatus();
    return false;
  }
}

Future<bool> pauseLocalRecording() async {
  if (!(isLocalRecording && !isLocalPauseRecording)) {
    return false;
  }

  try {
    await localRecorder!.pauseRecorder();
    isLocalPauseRecording = true;
    return true;
  } catch (e) {
    print(e);
    await _resetRecorderStatus();
    return false;
  }
}

Future<bool> resumeLocalRecording() async {
  if (!(isLocalRecording && isLocalPauseRecording)) {
    return false;
  }

  try {
    await localRecorder!.resumeRecorder();
    isLocalPauseRecording = false;
    return true;
  } catch (e) {
    print(e);
    await _resetRecorderStatus();
    return false;
  }
}

Future<String?> stopLocalRecording() async {
  if (!(localRecorder != null && isLocalRecording)) {
    throw Exception("录音器未初始化");
  }

  try {
    String? url = await localRecorder!.stopRecorder();
    isLocalRecording = false;
    isLocalPauseRecording = false;

    if (url == null || url == "") {
      throw Exception("录音失败");
    }

    await _resetRecorderStatus();

    return url;
  } catch (e) {
    print(e);
    await _resetRecorderStatus();
    throw Exception("录音失败");
  }
}

/// 关闭录音器,释放资源
void closeLocalRecorder() async {
  await _resetRecorderStatus();
}

Future<void> _resetRecorderStatus() async {
  try {
    await localRecorder?.closeRecorder();
  } catch (e) {
    print(e);
  }
  localRecorder == null;
  isLocalRecording = false;
  isLocalPauseRecording = false;
  localRecordedFilePath = null;
}

class AudioRecorderStartHandler extends MessageHandler {
  @override
  Future<dynamic> handleMessage(dynamic params) async {
    bool result = await startLocalRecording();
    return result;
  }
}

class AudioRecorderPauseHandler extends MessageHandler {
  @override
  Future<dynamic> handleMessage(dynamic params) async {
    bool result = await pauseLocalRecording();
    return result;
  }
}

class AudioRecorderResumeHandler extends MessageHandler {
  @override
  Future<dynamic> handleMessage(dynamic params) async {
    bool result = await resumeLocalRecording();
    return result;
  }
}

class AudioRecorderStopHandler extends MessageHandler {
  @override
  Future<dynamic> handleMessage(dynamic params) async {
    return await stopLocalRecording();
  }
}

class AudioRecorderClearHandler extends MessageHandler {
  @override
  Future<dynamic> handleMessage(dynamic params) async {
    closeLocalRecorder();
    return true;
  }
}