14

Is there any way to intercept or just-read the audio output in android device?

I need to read the whole audio output in PCM from inside myActivity, including media player application in background, voice from calls, MediaPlayer istances inside myACtivity, etc., everything that's going to played by speakers. Actually, if it was possible to read them separately, would be great as well.

I tried with AudioRecord, giving it as audioSource parameter every constant found in MediaRecorder.AudioSource with no luck, should I try different audioSources?

Is it a so low-level task that has to be handled within native layer?

lelloman
  • 13,883
  • 5
  • 63
  • 85

1 Answers1

11

The visualizer class is helpful. I use it to playback immediately played audio here.

This audio comes in very low quality, however, so it's only really good for visualization.

TheLivingForce
  • 532
  • 9
  • 21
  • Thanks for the input, I read your code and I was wondering, what is the audio input for the visualizer? I'm curious about this line `audioOutput = new Visualizer(0); // get output audio stream` – lelloman May 11 '15 at 08:18
  • 1
    As per the visualizer documentation: "Creating a Visualizer on the output mix (audio session 0)..." Basically, this creates the Visualizer based on the Android's audio output stream, not based on any independent output stream. Note that you'll need the permission MODIFY_AUDIO_SETTINGS in order for the constructor to acquire the android's output audio stream. – TheLivingForce May 12 '15 at 05:29
  • wow thanks, I'll give it a try as soon as possible, you might become my hero – lelloman May 12 '15 at 09:24
  • 1
    Don't thank me yet; I wasn't kidding when I said that the audio is very low quality. I'm going to try to reverse engineer it at some point to get higher quality, though. – TheLivingForce May 12 '15 at 23:45
  • 1
    Ok you were right, the quality is really poor but it was worth a shot anyway – lelloman May 25 '15 at 09:23
  • 1
    have you find a way to improve it's audio quality ? maybe some ways to edit the resulting pcm file with an audio editor software ? – Lord Sepid Feb 14 '16 at 11:43
  • 1
    Yeah, the problem comes down to this function [here](http://code.metager.de/source/xref/android/4.0.4/frameworks/base/media/libmedia/Visualizer.cpp) with the VISUALIZER_CAPTURE_SIZE_MAX. You'd need to change that definition or would need to go back to the setCaptureSize function [here](https://android.googlesource.com/platform/frameworks/base/+/56a2301/media/java/android/media/audiofx/Visualizer.java) and change the implementation of the native_setCaptureSize function (not sure how). I'd go forward and try to change the native_setCaptureSize function but I'm not sure how... – TheLivingForce Mar 30 '16 at 03:15
  • Can you please show how to record audio from the visualizer? – android developer Jun 24 '18 at 07:59
  • What you can do is call the setDataCaptureListener function on the visualizer. Inside this, you can write the waveform bytes to an audio recording session (something outside the scope of my knowledge; I only know how to do this in iOS). – TheLivingForce Jun 26 '18 at 18:15