skip to Main Content

I am using flutter_sound: ^9.2.13 package to record audio in my flutter app and it’s in File format : File: ‘/Users/Library/Developer/CoreSimulator/Devices/D8D77AE7-EE9B-4244-8707-98062B1F2FE1/data/Containers/Data/Application/B69AF519-A77A-4C5C-8D40-459003905B5F/tmp/audio’ it doesn’t have extension, and I have to send it to sever but server only accepts .mp3 file, how do I convert it and send it to server??

class PostComplainsPage extends StatefulWidget {
  const PostComplainsPage({Key? key}) : super(key: key);

  @override
  State<PostComplainsPage> createState() => _PostComplainsPageState();
}

class _PostComplainsPageState extends State<PostComplainsPage> {
  @override
  void initState() {
    initRecorder();
    super.initState();
  }

  @override
  void dispose() {
    recorder.closeRecorder();
    super.dispose();
  }

  bool _isChecked = false;
  bool visibleText = true;
  late int grievanceId;
  String? imageOne, title, description, name, email, phone;
  File? audioFile;
  final grievanceType = TextEditingController();
  final wardno = TextEditingController();
  final isprivate = TextEditingController();
  final recorder =
      FlutterSoundRecorder(); //? this is for recording audio using flutter sound package

  Future record() async {
    //todo this is for start recording audio
    await recorder.startRecorder(toFile: 'audio');
  }

  Future stop() async {
    //todo this is for stop recording audio
    final filePath = await recorder.stopRecorder();
    final fileAudio = File(filePath!);
    audioFile = fileAudio;
    print('Recorded file path : $audioFile');
  }

  Future initRecorder() async {
    final status = await Permission.microphone.request();
    if (status != PermissionStatus.granted) {
      context.showSnackbarError(message: 'Permission required');
    }
    await recorder.openRecorder();
    recorder.setSubscriptionDuration(const Duration(milliseconds: 500));
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return BlocProvider(
      create: (context) => getIt<PostGrievanceCubit>(),
      child: Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: false,
          title: const kAppBar(title: 'नयाँ गुनासो'),
        ),
        body: BlocListener<PostGrievanceCubit, PostGrievanceState>(
          listener: (context, state) {
            state.maybeWhen(
              orElse: () => null,
              success: (_) {
                context.showSnackbarSuccess(
                  margin: const EdgeInsets.only(right: 15, left: 15),
                  message: 'Gunaso has been posted',
                );
                context.router.replace(
                  const BottomNavigationRoute(),
                );
              },
              loading: () {
                const Center(
                  child: CircularProgressIndicator(
                    color: Colors.pink,
                  ),
                );
              },
              error: (message) {
                context.showSnackbarError(
                  margin: const EdgeInsets.only(right: 15, left: 15),
                  message: 'Failed, Please Try Again',
                );
              },
              noInternet: () => context.showSnackbarError(
                margin: const EdgeInsets.only(right: 15, left: 15),
                message: 'No Internet Connection',
              ),
            );
          },
          child: SingleChildScrollView(
            child: Column(
              children: [
                const SizedBox(
                  height: 15,
                ),
                Containers(
                  margin: const EdgeInsets.symmetric(horizontal: 15),
                  height: 30,
                  width: size.width,
                  child: const Texts(
                    texts: 'गुनसोकर्ताको जानकारी',
                    fontSize: 20,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                const SizedBox(
                  height: 15,
                ),
                Containers(
                  height: 30,
                  width: size.width,
                  child: Row(
                    children: [
                      Checkbox(
                        onChanged: (value) {
                          setState(
                            () {
                              _isChecked = value!;
                            },
                          );
                        },
                        activeColor: AppColor.btnColor,
                        value: _isChecked,
                      ),
                      const Texts(
                        texts: 'गुनसोकर्ताको नाम गोप्य राख्नुहोस',
                        fontSize: 16,
                      ),
                    ],
                  ),
                ),
              
                ),
                Column(
                  children: [
                    const SizedBox(
                      height: 20.0,
                    ),
                    imageOne != null
                        ? Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 15),
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(10),
                              child: Image.file(
                                File(imageOne!),
                                width: size.width,
                                height: 220,
                                fit: BoxFit.fill,
                              ),
                            ),
                          )
                        : const SizedBox.shrink(),
                    const SizedBox(
                      height: 10.0,
                    ),
                    Visibility(
                      visible: true,
                      child: Center(
                        child: Column(
                          children: [
                            Containers(
                              child: CircleAvatar(
                                backgroundColor: Colors.grey.shade200,
                                radius: 45,
                                child: const Center(
                                  child: Icon(
                                    Icons.image,
                                    size: 45,
                                    color: AppColor.btnColor,
                                  ),
                                ),
                              ),
                              onTap: () {
                                setState(
                                  () {
                                    visibleText = !visibleText;
                                  },
                                );
                                showModalBottomSheet<void>(
                                  context: context,
                                  builder: (BuildContext context) {
                                    return Container(
                                      height: 120.9,
                                      color: Colors.grey.shade200,
                                      child: Center(
                                        child: Column(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            const SizedBox(
                                              height: 10.0,
                                            ),
                                            GestureDetector(
                                              onTap: () async {
                                                final path = (await ImagePicker()
                                                        .pickImage(
                                                            source: ImageSource
                                                                .camera))
                                                    ?.path;
                                                if (path != null) {
                                                  imageOne = path;
                                                  if (mounted) {
                                                    setState(() {});
                                                  }
                                                }
                                              },
                                              child: Container(
                                                height: 40,
                                                width: size.width / 1.5,
                                                decoration: BoxDecoration(
                                                  color: Colors.grey,
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          20.0),
                                                ),
                                                child: Row(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .center,
                                                    children: [
                                                      const Text(
                                                        'Camera',
                                                      ),
                                                      const SizedBox(
                                                        width: 5.0,
                                                      ),
                                                      const Icon(Icons.camera)
                                                    ]),
                                              ),
                                            ),
                                            GestureDetector(
                                              onTap: () async {
                                                final path = (await ImagePicker()
                                                        .pickImage(
                                                            source: ImageSource
                                                                .gallery))
                                                    ?.path;
                                                if (path != null) {
                                                  imageOne = path;
                                                  if (mounted) {
                                                    setState(() {});
                                                  }
                                                }
                                              },
                                              child: Container(
                                                height: 40,
                                                width: size.width / 1.5,
                                                decoration: BoxDecoration(
                                                  color: Colors.grey,
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          20.0),
                                                ),
                                                child: Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.center,
                                                  children: [
                                                    const Text(
                                                      'Gallery',
                                                    ),
                                                    const SizedBox(
                                                      width: 5.0,
                                                    ),
                                                    const Icon(
                                                      Icons.image,
                                                      color: Colors.black,
                                                    )
                                                  ],
                                                ),
                                              ),
                                            ),
                                            const SizedBox(
                                              height: 10.0,
                                            ),
                                          ],
                                        ),
                                      ),
                                    );
                                  },
                                );
                              },
                            ),
                            const SizedBox(
                              height: 10,
                            ),
                            const Texts(
                              texts: 'इमेज',
                              fontSize: 22,
                            )
                          ],
                        ),
                      ),
                    ),
                    const SizedBox(
                      height: 5.0,
                    ),
                  ],
                ),
                const SizedBox(
                  height: 10,
                ),
                Column(
                  children: [
                    StreamBuilder<RecordingDisposition>(
                      stream: recorder.onProgress,
                      builder: (context, snapshot) {
                        final duration = snapshot.hasData
                            ? snapshot.data!.duration
                            : Duration.zero;
                        String twoDigits(int n) => n.toString().padLeft(2, '0');
                        final twoDigitsMinutes =
                            twoDigits(duration.inMinutes.remainder(60));
                        final twoDigitsSeconds =
                            twoDigits(duration.inSeconds.remainder(60));
                        return Texts(
                          texts: '$twoDigitsMinutes : $twoDigitsSeconds',
                          fontSize: 38,
                        );
                      },
                    ),
                    Containers(
                      onTap: () async {
                        if (recorder.isRecording) {
                          await stop();
                          setState(() {});
                        } else {
                          await record();
                          setState(() {});
                        }
                      },
                      child: CircleAvatar(
                        backgroundColor: Colors.grey.shade200,
                        radius: 45,
                        child: Center(
                          child: Icon(
                            recorder.isRecording
                                ? Icons.stop
                                : Icons.mic_none_rounded,
                            size: 48,
                            color: AppColor.btnColor,
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(
                      height: 10,
                    ),
                    const Texts(
                      texts: 'अडियो',
                      fontSize: 22,
                    )
                  ],
                ),
                const SizedBox(
                  height: 25,
                ),
                BlocBuilder<PostGrievanceCubit, PostGrievanceState>(
                  builder: (context, state) {
                    return Container(
                      margin: const EdgeInsets.symmetric(horizontal: 15),
                      child: CustomButton(
                        title: 'Submit',
                        loading: state.maybeWhen(
                          orElse: () => false,
                          loading: () => true,
                        ),
                        isDisabled: false,
                        onPressed: () async {
                          context.read<PostGrievanceCubit>().postGrievance(
                              title: title.toString(),
                              image: imageOne == null ? null : File(imageOne!),
                              audio: audioFile == null ? null : audioFile,
                              description: description.toString(),
                              wardno: int.parse(wardno.text),
                              name: name.toString(),
                              email: email.toString(),
                              phoneno: phone.toString(),
                              isprivate: _isChecked == false ? 0 : 1,
                              type: grievanceId);
                        },
                      ),
                    );
                  },
                ),
                const SizedBox(
                  height: 35,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

2

Answers


  1. You can use the following package to record voice as mp3 format:
    https://pub.dev/packages/record_mp3
    This package support both Android and iOS and will just make your life easier and you do not need to worry about converting the recorded file to mp3. Check the example tab in the package url, it is pretty straight forward.

    Login or Signup to reply.
  2. Try using the package record_mp3: https://pub.dev/packages/record_mp3/example

    As it would record the file in Mp3 format and then you can send this file as Mp3 to the server

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search