audio_plugin.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'dart:io';
  2. import 'dart:math';
  3. import 'package:assets_audio_player/assets_audio_player.dart';
  4. import 'package:flutter_sound/flutter_sound.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:permission_handler/permission_handler.dart';
  7. import '../interfaces/interfaces.dart';
  8. import 'package:path/path.dart' as pathP;
  9. import 'package:flutter/foundation.dart' show kIsWeb;
  10. class AudioPlugin implements AudioInterface {
  11. final _soundRecorder = FlutterSoundRecorder();
  12. final _soundPlayer = AssetsAudioPlayer();
  13. bool _isRecorderReady = false;
  14. String? pathGlobal = '';
  15. @override
  16. Future<void> audioPauseAndResume() async {
  17. _soundPlayer.playOrPause();
  18. }
  19. @override
  20. Future audioPlay(File audio) async {
  21. _soundPlayer.open(
  22. Audio.file(audio.path),
  23. autoStart: true,
  24. showNotification: true,
  25. );
  26. }
  27. @override
  28. Future initSoundRecorder() async {
  29. final microfono = await Permission.microphone.request();
  30. if (microfono != PermissionStatus.granted) {
  31. throw 'Sin permiso de microfono';
  32. }
  33. await _soundRecorder.openRecorder();
  34. _isRecorderReady = true;
  35. await _soundRecorder.setSubscriptionDuration(
  36. const Duration(milliseconds: 500),
  37. );
  38. }
  39. Future<void> closeSoundRecorder() async {
  40. await _soundRecorder.closeRecorder();
  41. }
  42. @override
  43. Future startRecordAudio() async {
  44. print("1.-");
  45. if (!_isRecorderReady) return;
  46. await Permission.microphone.request();
  47. if (!kIsWeb) {
  48. await Permission.storage.request();
  49. await Permission.manageExternalStorage.request();
  50. }
  51. print("2.-");
  52. final random = Random();
  53. final randomInt = random.nextInt(99999);
  54. pathGlobal = '$randomInt.wav';
  55. print("3.-");
  56. if (!kIsWeb) {
  57. pathGlobal = '/$randomInt.wav';
  58. String directorio = (await getApplicationDocumentsDirectory()).path;
  59. Directory dir =
  60. Directory(pathP.dirname(directorio + pathGlobal.toString()));
  61. if (!dir.existsSync()) {
  62. dir.createSync();
  63. }
  64. pathGlobal = directorio + pathGlobal.toString();
  65. print("el global");
  66. print(pathGlobal);
  67. }
  68. print("4.-");
  69. await _soundRecorder.startRecorder(
  70. codec: Codec.pcm16WAV,
  71. numChannels: 1,
  72. sampleRate: 44000,
  73. toFile: pathGlobal.toString(),
  74. );
  75. }
  76. @override
  77. Future<File?> stopRecordAudio() async {
  78. if (!_isRecorderReady) return await Future.value(null);
  79. final path = await _soundRecorder.stopRecorder();
  80. return File(pathGlobal.toString());
  81. }
  82. bool isRecording() {
  83. if (_soundRecorder.isRecording) {
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. Stream<RecordingDisposition>? recordOnProgress() {
  90. return _soundRecorder.onProgress;
  91. }
  92. }