Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - record sound and check sound volume

Tags:

android

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.

like image 804
DAVIDBALAS1 Avatar asked Aug 29 '16 16:08

DAVIDBALAS1


People also ask

Can I record internal audio in Android?

Scroll down to the Audio settings and choose to record Internal audio (Android 10+). Go to settings and choose internal audio. ADV comes with a floating button that allows you to stop and start recordings without entering your notification shade. It can be deactivated if you don't want to have it in your screencasts.

How do I record internal and external audio on my Android phone?

Here's how to record external and internal audio on your Android device. Use your phone's microphone and an audio recorder app to record external audio on Android. You can use the phone's built-in screen recorder feature or download an app from the Google Play Store to record internal audio.


1 Answers

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

like image 95
Hitesh Sahu Avatar answered Nov 06 '22 06:11

Hitesh Sahu