Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error starting recording: PlatformException(startRecorder, startRecorder, Failure to start recorder, null) #992

Closed
mazab99 opened this issue Nov 15, 2023 · 2 comments

Comments

@mazab99
Copy link

mazab99 commented Nov 15, 2023

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

@OverRide
State createState() => _UploadAudioScreenState();
}

class _UploadAudioScreenState extends State {
List pickedAudios = [];
List pickedAudiosNames = [];
final FlutterSoundRecorder _recorder = FlutterSoundRecorder();

final FlutterSoundPlayer _player = FlutterSoundPlayer();

String _recordedFilePath = '';

Future startRecording() async {
try {
await _recorder.openRecorder();

  Directory? externalDir = await getExternalStorageDirectory();
  String downloadFolderPath = externalDir!.path;

  _recordedFilePath = '$downloadFolderPath/my_sound.aac';

  await _recorder.startRecorder(
    toFile: _recordedFilePath,
  );

  print('Recording started to: $_recordedFilePath');
} catch (error) {
  print('Error starting recording: $error');
}

}

Future stopRecording() async {
try {
await _recorder.stopRecorder();
await _recorder.closeRecorder();
setState(() {
_recordedFilePath = _recordedFilePath;
});
print('Recording stopped');
} catch (error) {
print('Error stopping recording: $error');
}
}

Future playRecording() async {
try {
await _player.openPlayer();
await _player.startPlayer(
fromURI: _recordedFilePath,
codec: Codec.aacADTS,
);
print('Playback started');
} catch (error) {
print('Error playing recording: $error');
}
}

Future stopPlaying() async {
try {
await _player.stopPlayer();
await _player.closePlayer();
setState(() {
_recordedFilePath = _recordedFilePath;
});
print('Playback stopped');
} catch (error) {
print('Error stopping playback: $error');
}
}

String getRecordedFileName() {
return _recordedFilePath.isNotEmpty
? _recordedFilePath.split('/').last
: 'No recording yet';
}

Future pickAudio() async {
try {
final FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['mp3', 'wav', 'aac', 'ogg', 'flac'],
allowMultiple: true,
);
if (result != null) {
setState(() {
for (var audio in result.files) {
pickedAudios.add(File(audio.path!));
pickedAudiosNames.add(audio.name);
}
});
} else {
setState(() {
for (var audio in result!.files) {
pickedAudios.add(File(audio.path!));
pickedAudiosNames.add(audio.name);
}
});
}
} catch (error) {
print(error);
}
}

Future uploadAudio({
required List paths,
required List names,
}) async {
try {
FormData formData = FormData();
for (int i = 0; i < paths.length; i++) {
formData.files.add(
MapEntry(
'audios[]',
await MultipartFile.fromFile(
paths[i],
filename: names[i],
),
),
);
}
Response response = await Dio().post(
'https://file.io/?expires=1w',
data: formData,
options: Options(
contentType: 'multipart/form-data',
),
onSendProgress: (count, total) {
print('onSendProgress.......');
},
);
print(response.data);
} catch (error) {
if (error is DioException) {
print(error.response);
}
print(error);
}
}

@OverRide
void dispose() {
stopPlaying();
super.dispose();
}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Upload Audio'),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
MaterialButton(
onPressed: () async {
await pickAudio();
},
color: Colors.red,
child: const Text(
'Pick Audio',
style: TextStyle(color: Colors.white),
),
),
const SizedBox(width: 20),
MaterialButton(
onPressed: () {
uploadAudio(
paths: pickedAudios.map((e) => e.path).toList(),
names: pickedAudiosNames,
);
},
color: Colors.red,
child: const Text(
'Upload Picked Audio',
style: TextStyle(color: Colors.white),
),
),
],
),
const SizedBox(height: 20),
const Text(
'Picked Audios:',
style: TextStyle(fontSize: 16),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: pickedAudiosNames.map((name) {
return Text(
'- $name',
style: const TextStyle(fontSize: 16),
);
}).toList(),
),
const SizedBox(height: 50),
Row(
children: [
MaterialButton(
onPressed: () async {
await startRecording();
},
color: Colors.red,
child: const Text(
'Start Recording',
style: TextStyle(color: Colors.white),
),
),
const SizedBox(width: 20),
MaterialButton(
onPressed: () async {
await stopRecording();
},
color: Colors.red,
child: const Text(
'Stop Recording',
style: TextStyle(color: Colors.white),
),
),
],
),
const SizedBox(height: 20),
Row(
children: [
MaterialButton(
onPressed: () async {
await playRecording();
},
color: Colors.red,
child: const Text(
'Play Recording',
style: TextStyle(color: Colors.white),
),
),
const SizedBox(width: 20),
MaterialButton(
onPressed: () async {
await stopPlaying();
},
color: Colors.red,
child: const Text(
'Stop Playing',
style: TextStyle(color: Colors.white),
),
),
],
),
const SizedBox(height: 50),
const Text(
'Recorded File Path:',
style: TextStyle(fontSize: 16, color: Colors.red),
),
const SizedBox(height: 20),
Text(
_recordedFilePath.isNotEmpty
? _recordedFilePath
: 'No recording yet',
style: const TextStyle(fontSize: 16),
),
const SizedBox(height: 20),
const Text(
'Recorded File Name:',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 20),
Text(
getRecordedFileName(),
style: const TextStyle(fontSize: 16),
),
const SizedBox(height: 50),
MaterialButton(
onPressed: () {
uploadAudio(
paths: [
_recordedFilePath,
],
names: [
getRecordedFileName(),
],
);
},
color: Colors.red,
child: const Text(
'Upload Recorded Audio',
style: TextStyle(color: Colors.white),
),
),
],
),
),
);
}
}``

Copy link

This issue is stale because it has been open 90 days with no activity. Leave a comment or this will be closed in 7 days.

Copy link

github-actions bot commented Mar 6, 2024

This issue is stale because it has been open 365 days with no activity. Leave a comment or this will be closed in 7 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants