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.
source share