DAVIDBALAS1
DAVIDBALAS1

Reputation: 484

Android - record sound and check sound volume

I am trying to find out if my device is recording audio correctly (Volume of recorded audio is not too low and actually the recorded file has sound). The way I tried doing it is:

start recording --> play sound --> stop recording --> get file recorded max volume

The code I used to record 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 can't find out how to analyze the mp3 file created and check if it is not empty (from sound), is there a library or another way?

I hope you guys understood what I am trying to achieve as my English is pretty bad, Thanks.

EDIT:(Some more explaination) If you play sound (ringtone or something) while recording sound from microphone, the decibels recorded should be around 90 decibels. meaning the sound playing working and also the microphone, but if the decibels recorded around 30 means only microphone is working, and playing sound not, if the decibels are around zero then the microphone is not working.

Upvotes: 6

Views: 2304

Answers (1)

Hitesh Sahu
Hitesh Sahu

Reputation: 45160

You can use a visualiser to visualise real time if recording sound is getting too low or too loud.

I have build a project which visualise recording sound strength via bar graph . Higher the bar louder the recorded sound lower the bar low decibels .

This project also have inapp player which allow user to play all his recordings. The inbuilt player also visualise playback sound data.

I am suggesting this because I thought this is what you are trying to achieve in start recording --> play sound --> stop recording --> get file recorded max volume.

Instead of getting max volume each time you can rely on visualiser to keep an eye on recorder if recording file is getting recorded above acceptable decibals.

You can find source code on github

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

Upvotes: 1

Related Questions