java.lang.Object | |
↳ | android.media.audiofx.Visualizer |
The Visualizer class enables application to retrieve part of the currently playing audio for visualization purpose. It is not an audio recording interface and only returns partial and low quality audio content. However, to protect privacy of certain audio data (e.g voice mail) the use of the visualizer requires the permission android.permission.RECORD_AUDIO.
The audio session ID passed to the constructor indicates which audio content should be
visualized:
MediaPlayer
or
AudioTrack
using this audio session is visualized Two types of representation of audio content can be captured:
getWaveForm(byte[])
methodgetFft(byte[])
methodThe length of the capture can be retrieved or specified by calling respectively
getCaptureSize()
and setCaptureSize(int)
methods. The capture size must be a
power of 2 in the range returned by getCaptureSizeRange()
.
In addition to the polling capture mode described above with getWaveForm(byte[])
and
getFft(byte[])
methods, a callback mode is also available by installing a listener by
use of the setDataCaptureListener(OnDataCaptureListener, int, boolean, boolean)
method.
The rate at which the listener capture method is called as well as the type of data returned is
specified.
Before capturing data, the Visualizer must be enabled by calling the
setEnabled(boolean)
method.
When data capture is not needed any more, the Visualizer should be disabled.
It is good practice to call the release()
method when the Visualizer is not used
anymore to free up native resources associated to the Visualizer instance.
Creating a Visualizer on the output mix (audio session 0) requires permission
MODIFY_AUDIO_SETTINGS
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Visualizer.OnDataCaptureListener | The OnDataCaptureListener interface defines methods called by the Visualizer to periodically update the audio visualization capture. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | ALREADY_EXISTS | Internal opreation status. | |||||||||
int | ERROR | Unspecified error. | |||||||||
int | ERROR_BAD_VALUE | Operation failed due to bad parameter value. | |||||||||
int | ERROR_DEAD_OBJECT | Operation failed due to dead remote object. | |||||||||
int | ERROR_INVALID_OPERATION | Operation failed because it was requested in wrong state. | |||||||||
int | ERROR_NO_INIT | Operation failed due to bad object initialization. | |||||||||
int | ERROR_NO_MEMORY | Operation failed due to lack of memory. | |||||||||
int | STATE_ENABLED | State of a Visualizer object that is active. | |||||||||
int | STATE_INITIALIZED | State of a Visualizer object that is ready to be used. | |||||||||
int | STATE_UNINITIALIZED | State of a Visualizer object that was not successfully initialized upon creation | |||||||||
int | SUCCESS | Successful operation. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Class constructor.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns current capture size.
| |||||||||||
Returns the capture size range.
| |||||||||||
Get current activation state of the visualizer.
| |||||||||||
Returns a frequency capture of currently playing audio content.
| |||||||||||
Returns the maximum capture rate for the callback capture method.
| |||||||||||
Returns the sampling rate of the captured audio.
| |||||||||||
Returns a waveform capture of currently playing audio content.
| |||||||||||
Releases the native Visualizer resources.
| |||||||||||
Sets the capture size, i.e.
| |||||||||||
Registers an OnDataCaptureListener interface and specifies the rate at which the capture
should be updated as well as the type of capture requested.
| |||||||||||
Enable or disable the visualization engine.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Called before the object's memory is reclaimed by the VM.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Internal opreation status. Not returned by any method.
Operation failed due to bad parameter value.
Operation failed due to dead remote object.
Operation failed because it was requested in wrong state.
Operation failed due to bad object initialization.
Operation failed due to lack of memory.
State of a Visualizer object that is active.
State of a Visualizer object that is ready to be used.
State of a Visualizer object that was not successfully initialized upon creation
Successful operation.
Class constructor.
audioSession | system wide unique audio session identifier. If audioSession is not 0, the visualizer will be attached to the MediaPlayer or AudioTrack in the same audio session. Otherwise, the Visualizer will apply to the output mix. |
---|
java.lang.UnsupportedOperationException | |
RuntimeException | |
UnsupportedOperationException |
Returns the capture size range.
Get current activation state of the visualizer.
Returns a frequency capture of currently playing audio content.
This method must be called when the Visualizer is enabled.
The capture is an 8-bit magnitude FFT, the frequency range covered being 0 (DC) to half of
the sampling rate returned by getSamplingRate()
. The capture returns the real and
imaginary parts of a number of frequency points equal to half of the capture size plus one.
Note: only the real part is returned for the first point (DC) and the last point (sampling frequency / 2).
The layout in the returned byte array is as follows:
Index | 0 | 1 | 2 | 3 | 4 | 5 | ... | n - 2 | n - 1 |
Data | Rf0 | Rf(n/2) | Rf1 | If1 | Rf2 | If2 | ... | Rf(n-1)/2 | If(n-1)/2 |
fft | array of bytes where the FFT should be returned |
---|
SUCCESS
in case of success,
ERROR_NO_MEMORY
, ERROR_INVALID_OPERATION
or ERROR_DEAD_OBJECT
in case of failure.IllegalStateException |
---|
Returns the maximum capture rate for the callback capture method. This is the maximum value
for the rate parameter of the
setDataCaptureListener(OnDataCaptureListener, int, boolean, boolean)
method.
Returns the sampling rate of the captured audio.
IllegalStateException |
---|
Returns a waveform capture of currently playing audio content. The capture consists in
a number of consecutive 8-bit (unsigned) mono PCM samples equal to the capture size returned
by getCaptureSize()
.
This method must be called when the Visualizer is enabled.
waveform | array of bytes where the waveform should be returned |
---|
SUCCESS
in case of success,
ERROR_NO_MEMORY
, ERROR_INVALID_OPERATION
or ERROR_DEAD_OBJECT
in case of failure.IllegalStateException |
---|
Releases the native Visualizer resources. It is a good practice to release the visualization engine when not in use.
Sets the capture size, i.e. the number of bytes returned by getWaveForm(byte[])
and
getFft(byte[])
methods. The capture size must be a power of 2 in the range returned
by getCaptureSizeRange()
.
This method must not be called when the Visualizer is enabled.
size | requested capture size |
---|
SUCCESS
in case of success,
ERROR_BAD_VALUE
in case of failure.IllegalStateException |
---|
Registers an OnDataCaptureListener interface and specifies the rate at which the capture should be updated as well as the type of capture requested.
Call this method with a null listener to stop receiving the capture updates.
listener | OnDataCaptureListener registered |
---|---|
rate | rate in milliHertz at which the capture should be updated |
waveform | true if a waveform capture is requested: the onWaveFormDataCapture() method will be called on the OnDataCaptureListener interface. |
fft | true if a frequency capture is requested: the onFftDataCapture() method will be called on the OnDataCaptureListener interface. |
SUCCESS
in case of success,
ERROR_NO_INIT
or ERROR_BAD_VALUE
in case of failure.
Enable or disable the visualization engine.
enabled | requested enable state |
---|
SUCCESS
in case of success,
ERROR_INVALID_OPERATION
or ERROR_DEAD_OBJECT
in case of failure.IllegalStateException |
---|
Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.
The method can be used to free system resources or perform other cleanup
before the object is garbage collected. The default implementation of the
method is empty, which is also expected by the VM, but subclasses can
override finalize()
as required. Uncaught exceptions which are
thrown during the execution of this method cause it to terminate
immediately but are otherwise ignored.
Note that the VM does guarantee that finalize()
is called at most
once for any object, but it doesn't guarantee when (if at all) finalize()
will be called. For example, object B's finalize()
can delay the execution of object A's finalize()
method and
therefore it can delay the reclamation of A's memory. To be safe, use a
ReferenceQueue
, because it provides more control
over the way the VM deals with references during garbage collection.