Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
869 stars 568 forks source link

[HELP]: How to record .mp3 file. #898

Closed good-good-study closed 2 years ago

good-good-study commented 2 years ago

How to record .mp3 file ?

Larpoux commented 2 years ago

It is not possible to record mp3 with Flutter Sound on ios, android and web. Sorry!

good-good-study commented 2 years ago

toStream can only be used with codec == Codec.pcm16 What other audio formats are supported toStream?

Hello, the function I need to implement is to record the audio within 60s and convert it into text while recording. In addition to the pcm16 format, there are other formats supported ?

Thanks!

Larpoux commented 2 years ago

Sorry, record to stream only works with pcm16. You could try to encode the pcm16 data to mp3 with a mp3 encoder like 'lame'. This would be a major work. If you do something like that, I suggest that you publish your solution to pub.dev, because other developers will probably be interested by your work

Larpoux commented 2 years ago

Note : if you use 'startRecorderToFile()' and not 'startRecorderToStream()' you can:

This is very simple, but you will not get a stream on the fly. Again, if you want a real time mp3 stream you really must use a mp3 encoder like 'lame'. ('lame' is included with the FFmeg Kit plugin). This will be much more complicated than converting a Wave file to a MP3 file with just a very simple call to FFmpeg.

If you want to get mp3 files from 60 seconds batch records, I guess that the solution to call ffmpeg to convert WAVE files to MP3 files is certainly what you need and will be very simple to implement.

shobith10 commented 2 years ago

which version of flutter sound is more compatible

Larpoux commented 2 years ago

The last version 9.x is compatible with 'FFmpeg kit' plugin.

shobith10 commented 2 years ago

Future record() async { if (!isRecoderReady.value) return; Directory tempDir = await getTemporaryDirectory();

await recorder.startRecorder(
    toFile: 'audio', codec: audio.Codec.defaultCodec);

}

Future stop() async { if (!isRecoderReady.value) return;

final path = await recorder.stopRecorder();

String fileName = path!.split('/').last;

FFmpegKit.execute('-i $path -c:a mp3 $path.mp3').then((session) async {
  final returnCode = await session.getReturnCode();
  if (ReturnCode.isSuccess(returnCode)) {
    print('success');
  } else {
    print('error');
  }
});

am always getting print error, how actually to use this package with lame encoder ?

Larpoux commented 2 years ago

await recorder.startRecorder( toFile: 'audio', codec: audio.Codec.defaultCodec);

You can try something like: ''' String path = '$tempdir/audio' ; startRecorder(toFile: '$path.wav', codec: Codec. pcmWav) ; ... FFmpegKit.execute('-i $path.wav $path.mp3').then(( ... '''

shobith10 commented 2 years ago

i tried startRecoding it records correctly, but path returned on stropRecorder is empty or says no such file directory

final path = await recorder.stopRecorder();

shobith10 commented 2 years ago

this method worked for me , its better to use aac format than mp3, it supports almost all kinds browsers
..... Directory tempDir = await getApplicationDocumentsDirectory(); String path = tempDir.path + '/audio.aac';

print("directory path: $path");

//audioFile = await File('${tempDir.path}/audio.mp3');
await recorder.startRecorder(toFile: path, codec: audio.Codec.aacMP4);

}

zhouxin1233 commented 2 years ago

await recorder.startRecorder( toFile: 'audio', codec: audio.Codec.defaultCodec);

You can try something like: ''' String path = '$tempdir/audio' ; startRecorder(toFile: '$path.wav', codec: Codec. pcmWav) ; ... FFmpegKit.execute('-i $path.wav $path.mp3').then(( ... '''

it‘s not work,

var ffmpegSession = await FFmpegKit.execute('-i $sourcePath $convertResultPath.mp3');

Automatic encoder selection failed for output stream #0:0. Default encoder for format mp3 (codec mp3) is probably disabled. Please choose an encoder manually. E/log FlutterLog convertAudio (27327): Error selecting an encoder for stream 0:0