14

I know Visualizer to show some wave while playing audio using android Media Player.

But i want to show Visualizer while recording audio means while recording i want to show linear wave which changes based on user voice beat.

Is it possible to do in android.

koti
  • 3,681
  • 5
  • 34
  • 58

3 Answers3

19

by calling every x milliseconds your MediaRecorder.getMaxAmplitude(), you gonna have (from official documentation) :

the maximum absolute amplitude that was sampled since the last call to this method.

then, you can process this value in real time to draw a graph or change some view properties.
not perfect, but I hope it helps =)

edit: just so you know, the retrieved value will be the same across all android devices : between 0 and 32767. (I have more than 10k user's reports giving me this value when they blow in the mic).

Community
  • 1
  • 1
elgui
  • 3,303
  • 4
  • 28
  • 37
  • it need to specify the output file.. Can it be skipped? – Shashank Kumar Sep 10 '14 at 11:57
  • no it can't, just give the recorder one like this : File cacheDir = getWritableCacheDir(); samplePath = cacheDir.getAbsolutePath() + "/sample"; mRecorder.setOutputFile(samplePath); and delete it when you're done... – elgui Sep 10 '14 at 15:17
  • @ShashankKumar: or use the AudioRecorder. (no file needed) – njzk2 Oct 30 '14 at 21:03
  • how to use this method with Visualizer class, can you explain in detail or can you provide any sample code which can show all this functionality – Sahil Garg Sep 15 '15 at 08:04
3

You may need to use AudioRecorder class instead of MediaRecorder.

Check AudioRecorder#read(...) methods which put audio data to byte[] instead of putting it directly to a file.

To show changes on the graph you will have to analyze the data (which is encoded in PCM 8 or 16 bit - link) and update the graph in real time.

MeTTeO
  • 2,088
  • 16
  • 19
3

Two important things:

  • you need to convert live bytes (from mic) to numeric values inorder to plot them.
  • Since you use a real-time graph, to plot those points use SurfaceView.

    Convert recording bytes to numeric values refer: Android: Listener to record sound if any sound occurs where you will see the variable "temp" holds the numerical value of your audio.

    Plot points These numeric values which indicates your Y values is plotted against increasing X (time interval) values (0,1,2..) as graph. Using SurfaceView eg..,

//canvas.drawLine(previous X value,previous Y value,X,Y, paint);
canvas.drawPoint(X,Y,paint);
SurfaceHolder.unlockCanvasAndPost(canvas);

You need not plot all values, for efficiency you can filter those values with your conditions and plot for certain intervals of time.

Hope this helps :)

Community
  • 1
  • 1
everlasto
  • 4,890
  • 4
  • 24
  • 31