audio_plugin.dart 2.7 KB

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