bsutton / sounds

Flutter plugin for sound. Audio recorder and player.
GNU Lesser General Public License v3.0
78 stars 25 forks source link

Setting audio recording quality #38

Closed hanllo closed 4 years ago

hanllo commented 4 years ago

I'm having some trouble changing the sampleRate and bitRate of recordings from their default values of 16000. If anyone has done this and could help me out I'd really appreciate it.

Here's my startRecording function, in which I set the mediaFormat including sampleRate/bitRate as shown in the SoundRecorder documentation:

Future<void> startRecording() async {
    try {
      String file = Track.tempFile(WellKnownMediaFormats.adtsAac);
      track = Track.fromFile(file,
          mediaFormat: AdtsAacMediaFormat(
            sampleRate: 48000,
            numChannels: 1,
            bitRate: 48000,
          ));
      await recorder.record(track);
      print('startRecorder: $track');

      setState(() => isRecording = true);
    } on RecorderException catch (err) {
      print('startRecorder error: $err');

      var error = SnackBar(
          backgroundColor: Colors.red,
          content: Text('Failed to start recording: $err'));
      Scaffold.of(context).showSnackBar(error);

      stopRecording();
      setState(() => isRecording = false);
    }
  }

Here's the relevant output:

I/flutter (21449): startRecorder:   audio: MediaFormat: adts/aacstorage: /data/user/0/com.example.mobile_prototype/code_cache/62b29fcf-987b-4cb7-912a-516a46965d40.aac path: /data/user/0/com.example.mobile_prototype/code_cache/62b29fcf-987b-4cb7-912a-516a46965d40.aac
D/SoundRecorder(21449): startRecorder: /data/user/0/com.example.mobile_prototype/code_cache/62b29fcf-987b-4cb7-912a-516a46965d40.aac
E/SensorManager(21449): registerListenerImpl sensorName:LSM6DS3 Accelerometer,isWakeUpSensor:false,callingApp: com.example.mobile_prototype,callingPid:21449,callingUid:10371
I/flutter (21449): 11 16:34:34.762 Debug base_plugin.dart : BasePlugin.invokeMethod : 114 ::: invokeMethod returned for startRecorder slot: 0
V/MediaRecorder(21449): constructor
V/MediaRecorder(21449): doCleanUp
V/MediaRecorder(21449): setListener
V/MediaRecorder(21449): setClientName
V/MediaRecorder(21449): reset
V/MediaRecorder(21449): doCleanUp
V/MediaRecorder(21449): setAudioSource(1)
V/MediaRecorder(21449): Call init() since the media recorder is not initialized yet
V/MediaRecorder(21449): init
V/MediaRecorder(21449): setOutputFormat(6)
V/MediaRecorder(21449): setAudioEncoder(3)
V/MediaRecorder(21449): setParameters(audio-param-number-of-channels=1)
V/MediaRecorder(21449): setParameters(audio-param-sampling-rate=16000)
V/MediaRecorder(21449): setParameters(audio-param-encoding-bitrate=16000)
V/MediaRecorder(21449): setOutputFile(78)
V/MediaRecorder(21449): prepare
E/MediaRecorder(21449): start 0 m_rotation
V/MediaRecorder(21449): start
E/SensorManager(21449): unregisterListenerImpl callingApp: com.example.mobile_prototype,callingPid:21449,callingUid:10371
V/MediaRecorder(21449): getMaxAmplitude
D/OnePlusJankManager(21449):  Chor uploadMDM JANK_TYPE_ONCE mViewTitle = com.example.mobile_prototype/com.example.mobile_prototype.MainActivity--- jank level = 1
V/MediaRecorder(21449): getMaxAmplitude
bsutton commented 4 years ago

You code looks correct so it looks like a bug.

relf108 commented 4 years ago

I've fixed this issue in the latest push to master but need to finalize some large changes to the iOS aspect of the project so I won't be doing doing a release until those are complete. That being said if you're in a hurry and not worried about iOS for the moment you could clone the code in master or if you already have a local copy of the API make the changes yourself as they are quite simple. The files that need to be changed are under sounds -> lib > media_format.

For each file ending in media_format paste sampleRate: sampleRate, numChannels: numChannels, bitRate: bitRate, Into each of the constructors where the args are being passed up to the parent class. Constructors should end up looking like this const AdtsAacMediaFormat({ int sampleRate = 16000, int numChannels = 1, int bitRate = 16000, }) : super.detail( name: 'adts/aac', sampleRate: sampleRate, numChannels: numChannels, bitRate: bitRate, );