You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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();
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();
}
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),
),
),
],
),
),
);
}
}``
The text was updated successfully, but these errors were encountered: