1

When using MediaRecorder to record audio, how can I know when a sound is being made and recorded?

Is there any listener? Maybe a way to get notified when a sound that is high enough is being recorded?

Or maybe I need to use a lower-level API for this? If so, can I still use the same functionality that's available on MediaRecorder, such as setAudioSource, setOutputFormat, setOutputFile and setAudioEncoder ?

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • There is no callbacks in MediaRecorder which say you "loud sound has been just recorded". So you need to analyze recorded sound by yourself. If you use usual PCM for audio recording you will be able to just sum 16bit values on some interval and if this sum exceeds some value it means that sound is loud. – Yev Kanivets Apr 12 '16 at 08:53
  • @evgenii Can you please show how to do it? And also how to still be able to use the functions I've mentioned ? What do you mean by "PCM" ? I wish to use various AudioSource values for this. Also, suppose I use MediaRecorder, is it possible to analyze the output file, after it was created, to check the sounds as I've written? – android developer Apr 12 '16 at 09:00
  • https://en.wikipedia.org/wiki/Pulse-code_modulation. PCM is a base not compresses format (used in WAV with some format header). In few words, this is just sequence of 16 bit every of which represents a sound value for some moment of record. This value correlates with loudness, so bigger value means louder sound. – Yev Kanivets Apr 12 '16 at 09:04
  • I think you can read from file you are writing to. – Yev Kanivets Apr 12 '16 at 09:05
  • @evgenii OK, how do I do it in runtime, as it records? Do you know perhaps of a tutorial/sample for this? Is AudioSource still available there? – android developer Apr 12 '16 at 09:15
  • 1
    Don't know about AudioSource, take a look on documentation for AudioRecord. Here is an example http://stackoverflow.com/questions/8499042/android-audiorecord-example. – Yev Kanivets Apr 12 '16 at 09:20
  • @evgenii Thank you. Looking at the code, I see that the CTOR has AudioSource, but where in it should I check the volume? And, will the output be uncompressed, as it's PCM format? Is it possible to avoid, and use 3gp for example? If not, is it possible to avoid creation of a file, and just check the audio as it does (fake) recording? How would you also check the output file, if I'd choose the approach of firstly record to file and then check what it has? – android developer Apr 12 '16 at 09:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108908/discussion-between-evgenii-and-android-developer). – Yev Kanivets Apr 12 '16 at 09:33
  • @evgenii sadly I don't have the right time for a chat. – android developer Apr 12 '16 at 12:33

0 Answers0