CodeHunter
CodeHunter

Reputation: 13

record audio but save it if it has data on android

I am recording audio on android but the main issue I am facing right now is that I don't want to save audio file if audio has no data in it (like the user has said nothing). I can save it then check the time or length of the audio file and then delete it, but I want to check it before storing file to reduce the work and improve performance.

Upvotes: 1

Views: 134

Answers (2)

Stephan Branczyk
Stephan Branczyk

Reputation: 9375

Depending on ambient noise, checking the amplitude may not be enough. You may need to do a Fast Fourrier transform on the audio to separate the frequencies. This can be done with a minimal amount of buffering.

See video for a visual explanation of what a Fourrier Transform is. There is also this video. It's actually a super fascinating topic.

In any case, there is sample code for an audio visualizer in the following book. Here is the accompanying github site for that book. It may also contain some other audio-related code samples you may find useful.

Upvotes: 0

Arash GM
Arash GM

Reputation: 10395

You can access the amplitude of the voice stream on MediaRecorder by :

public double getAmplitude() {
    if (recorder != null)
        return  recorder.getMaxAmplitude();
    else
        return 0;
}

then you can check the range to detect if there is any valuable data (like talking) on the audio or not

Upvotes: 2

Related Questions