Android recording voice file wav to mp3 method example

  • 2021-10-15 11:36:53
  • OfStack

1. Android uses AudioRecord to suspend recording and transfer wav files to mp3 files. Because of the open source of android system, many manufacturers change the system source code indiscriminately. Xiaomi is the most disgusting here. Using android native AudioRecord recording can only save voice files in wav and pcm formats, but the wav voice file system recorded by Xiaomi mobile phone itself does not support it, so MediaPlayer cannot play files in wav format, but other mobile phones can. At this time, 10,000 grass mud horses galloped by. . .

2. There are currently two solutions to this problem:

Scheme 1: Transforming the recorded wav file into mp3 format, the disadvantage: Transcoding process can not take 1 part of time, can not record and save the mp3 format file in real time

Here, the third square library on gitHub can be used to realize the conversion from wav to mp3

gitHub Address: https://github.com/adrielcafe/AndroidAudioConverter

2.1 Initialization:


public class App extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    AndroidAudioConverter.load(this, new ILoadCallback() {
      @Override
      public void onSuccess() {
        // Great!
      }
      @Override
      public void onFailure(Exception error) {
        // FFmpeg is not supported by device
      }
    });
  }
}

2.2 Calling out the call, can be called when the recording is completed for transcoding. When the transcoding process needs 1, almost 1s can be converted into 1M, and wav file of 10M takes almost 10s time to convert into mp3, and the file is reduced by more than 10 times after being converted into mp3


File flacFile = new File(Environment.getExternalStorageDirectory(), "my_audio.flac");
IConvertCallback callback = new IConvertCallback() {
  @Override
  public void onSuccess(File convertedFile) {
    // So fast? Love it!
  }
  @Override
  public void onFailure(Exception error) {
    // Oops! Something went wrong
  }
};
AndroidAudioConverter.with(this)
  // Your current audio file
  .setFile(flacFile) 
  
  // Your desired audio format 
  .setFormat(AudioFormat.MP3)
  
  // An callback to know when conversion is finished
  .setCallback(callback)
  
  // Start conversion
  .convert();

2.3 The configuration in gradle is as follows:


repositories {
 maven {
  url "https://jitpack.io"
 }
}

dependencies {
 compile 'com.github.adrielcafe:AndroidAudioConverter:0.0.8'
}

Option 2:

When Recorder in AudioRecord or MediaRecorder is recorded, it is directly recorded into MP3 or AAC audio files.

Because MP3 encoding is not supported by Android by default, many mature solutions on the Internet are to use Lame library to generate MP3 audio files through Lame encoding.

Here is a library of gitHub: https://github.com/lijunzz/Recorder-Android, which is relatively new and adapted to as3.1, that is, the number of star is a little less, so it can be used barely

After that, we will continue to optimize and summarize the problems encountered. . .


Related articles: