Android - record sound and check sound volume

I am trying to find out if the sound of my device is being recorded correctly (the volume of the recorded sound is not too low, and in fact the recorded file has sound). I tried to do this:

start recording → play sound → stop recording → get the maximum amount of recorded file

The code I used to record the sound:

public void playSound() { File myDataPath = new File(getActivity().getFilesDir().getAbsolutePath() + File.separator + ".CheckAudio"); if (!myDataPath.exists()) myDataPath.mkdirs(); recordFile = myDataPath + File.separator + "Recording_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + ".mp3"; am.setStreamVolume(AudioManager.STREAM_RING, am.getStreamMaxVolume(AudioManager.STREAM_RING), 0); am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0); Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); try { md = new MediaRecorder(); md.setAudioSource(MediaRecorder.AudioSource.MIC); md.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); md.setOutputFile(recordFile); md.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); md.prepare(); md.start(); } catch (IllegalStateException | IOException e) { recording = false; removeItem("Unable to record audio, please try again."); // (Show toast) return; } mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(getActivity(), defaultRingtoneUri); mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); mediaPlayer.prepare(); mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { md.stop(); md.release(); mediaPlayer.release(); mediaPlayer = null; // get recordfile volume } }); mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); removeItem("Unable to play audio"); sound = false; } } 

However, I cannot understand how to analyze the created mp3 file and check if it is empty (from the sound), is there a library or another way?

I hope you guys understand what I'm trying to achieve, because my English is pretty bad, thanks.

EDIT: (A few more explanations) If you play a sound (ringtone or something else) when recording sound from a microphone, the recorded decibels should be around 90 decibels. this means that the sound is working as well as the microphone, but if the decibels recorded are around 30, it means that only the microphone is working, and the sound does not play, if the decibels are around zero, then the microphone is not working.

+5
source share
1 answer

You can use the visualizer to render in real time if the recording sound becomes too low or too loud.

I have a project that visualizes the sound power of a recording through a histogram. The higher the volume level, the louder the recorded sound will lower the lower decibels.

This project also has an inapp player, which allows the user to play all his recordings. The built-in player also visualizes audio playback data.

I suggest this because I thought that this is what you are trying to achieve in start recording → play sound → stop recording → get the maximum amount of recorded file.

Instead of getting the maximum volume every time you can rely on the visualizer to keep track of the recorder if the recording file is recorded above the acceptable decibels.

You can find the source code on github

https://github.com/hiteshsahu/Android-Audio-Recorder-Visualization-Master

+1
source

All Articles