How to do Visualizer while recording audio in android
Asked Answered
N

3

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.

Nuncle answered 26/11, 2012 at 5:55 Comment(3)
would a level meter be enough, or do you need each frequencies level ?Dabchick
i need atleast some beat value to draw a graph my self using graph apiNuncle
plot recorded bytes with increasing intervals of time.. refer answer belowWickiup
D
20

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).

Dabchick answered 3/12, 2012 at 12:24 Comment(4)
it need to specify the output file.. Can it be skipped?Phrenology
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...Dabchick
@ShashankKumar: or use the AudioRecorder. (no file needed)Hoard
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 functionalityMerc
A
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.

Amphictyony answered 4/12, 2012 at 17:21 Comment(0)
W
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 :)

Wickiup answered 4/12, 2012 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.