Packagecom.terry.AudioFx;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.Rect;ImportAndroid.media.AudioManager;ImportAndroid.media.MediaPlayer;ImportAndroid.media.MediaPlayer.OnCompletionListener;ImportAndroid.media.audiofx.Equalizer;ImportAndroid.media.audiofx.Visualizer;ImportAndroid.media.audiofx.Visualizer.OnDataCaptureListener;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;Importandroid.widget.LinearLayout;ImportAndroid.widget.SeekBar;ImportAndroid.widget.TextView;ImportAndroid.widget.SeekBar.OnSeekBarChangeListener; Public classAudiofxactivityextendsActivity {Private Static FinalString TAG = "Audiofxactivity"; Private Static Final floatVisualizer_height_dip =50f; PrivateMediaPlayer Mmediaplayer; PrivateVisualizer Mvisualizer; PrivateEqualizer Mequalizer;//Equalization Device PrivateLinearLayout mlayout; Visualizerview Mvisualizerview; PrivateTextView Mstatustextview; Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setvolumecontrolstream (Audiomanager.stream_music); Mstatustextview=NewTextView ( This); Mlayout=NewLinearLayout ( This); Mlayout.setorientation (linearlayout.vertical); Mlayout.addview (Mstatustextview); Setcontentview (mlayout); Mmediaplayer= Mediaplayer.create ( This, r.raw.z8806c); Setupvisualizerfxandui (); Setupequalizefxandui (); Mvisualizer.setenabled (true); Mmediaplayer.setoncompletionlistener (NewOncompletionlistener () {@Override Public voidOncompletion (MediaPlayer MP) {//TODO auto-generated Method StubMvisualizer.setenabled (false); } }); Mmediaplayer.start (); Mstatustextview.settext ("Play in ... "); } /*** Create a priority 0 Equalizer object via the Audiosessionid returned by Mmediaplayer and generate the corresponding UI and corresponding events through the Spectrum*/ Private voidSetupequalizefxandui () {Mequalizer=NewEqualizer (0, Mmediaplayer.getaudiosessionid ()); Mequalizer.setenabled (true);//Enable EqualizerTextView Eqtextview =NewTextView ( This); Eqtextview.settext ("Equalizer:"); Mlayout.addview (Eqtextview); //get the Spectrum engine supported by the Equalizer ShortBands =mequalizer.getnumberofbands (); //Getbandlevelrange is an array that returns an array of spectral levels,//The first subscript is the lowest limit range//The second subscript is the maximum upper limit, which is taken out in turn Final ShortMinequalizer = Mequalizer.getbandlevelrange () [0]; Final ShortMaxequalizer = Mequalizer.getbandlevelrange () [1]; for( Shorti = 0; I < bands; i++) { Final ShortBand =i; TextView Freqtextview=NewTextView ( This); Freqtextview.setlayoutparams (Newviewgroup.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, Viewgroup.layoutpar Ams. Wrap_content)); Freqtextview.setgravity (gravity.center_horizontal); //Remove Center FrequencyFreqtextview SetText (Mequalizer.getcenterfreq (band)/+) + "HZ"); Mlayout.addview (Freqtextview); LinearLayout Row=NewLinearLayout ( This); Row.setorientation (linearlayout.horizontal); TextView Mindbtextview=NewTextView ( This); Mindbtextview.setlayoutparams (Newviewgroup.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, VIEWGROUP.LAYOUTPA Rams. Wrap_content)); Mindbtextview.settext ((Minequalizer/+) + "DB"); TextView Maxdbtextview=NewTextView ( This); Maxdbtextview.setlayoutparams (Newviewgroup.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, VIEWGROUP.LAYOUTPA Rams. Wrap_content)); Maxdbtextview.settext ((Maxequalizer/+) + "DB"); Linearlayout.layoutparams Layoutparams=Newlinearlayout.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, Viewgroup.layout Params.wrap_content); Layoutparams.weight= 1; SeekBar SeekBar=NewSeekBar ( This); Seekbar.setlayoutparams (Layoutparams); Seekbar.setmax (Maxequalizer-Minequalizer); Seekbar.setprogress (Mequalizer.getbandlevel (band)); Seekbar.setonseekbarchangelistener (NewOnseekbarchangelistener () {@Override Public voidOnstoptrackingtouch (SeekBar SeekBar) {} @Override Public voidOnstarttrackingtouch (SeekBar SeekBar) {} @Override Public voidOnprogresschanged (SeekBar SeekBar,intProgress,BooleanFromuser) { //TODO auto-generated Method StubMequalizer.setbandlevel (band, ( Short) (Progress +Minequalizer)); } }); Row.addview (Mindbtextview); Row.addview (Seekbar); Row.addview (Maxdbtextview); Mlayout.addview (row); } } /*** Generate a Visualizerview object so that the band of the audio spectrum can be reflected on the Visualizerview*/ Private voidSetupvisualizerfxandui () {Mvisualizerview=NewVisualizerview ( This); Mvisualizerview.setlayoutparams (NewViewgroup.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, (int) (VISUALIZER_HEIGHT_DIP *getresources (). Getdisplaymetrics (). density)); Mlayout.addview (Mvisualizerview); Mvisualizer=NewVisualizer (Mmediaplayer.getaudiosessionid ()); //the number of digits in the parameter must be 2Mvisualizer.setcapturesize (Visualizer.getcapturesizerange () [1]); //set allow waveform representation, and capture itMvisualizer.setdatacapturelistener (NewOndatacapturelistener () {@Override Public voidonwaveformdatacapture (Visualizer Visualizer,byte[] Waveform,intsamplingrate) { //TODO auto-generated Method StubMvisualizerview.updatevisualizer (waveform); } @Override Public voidOnfftdatacapture (Visualizer Visualizer,byte[] FFT,intsamplingrate) { //TODO auto-generated Method Stub}}, Visualizer.getmaxcapturerate ()/2,true,false); } @Overrideprotected voidOnPause () {//TODO auto-generated Method Stub Super. OnPause (); if(Isfinishing () && mmediaplayer! =NULL) {mvisualizer.release (); Mmediaplayer.release (); Mequalizer.release (); Mmediaplayer=NULL; } } classVisualizerviewextendsView {Private byte[] mBytes; Private float[] mpoints; //Rectangular Area PrivateRect Mrect =NewRect (); //Brushes PrivatePaint Mpaint =NewPaint (); //Initializing brushes Private voidinit () {mBytes=NULL; Mpaint.setstrokewidth (1f); Mpaint.setantialias (true); Mpaint.setcolor (Color.Blue); } PublicVisualizerview (Context context) {Super(context); Init (); } Public voidUpdatevisualizer (byte[] mbyte) {MBytes=MByte; Invalidate (); } @Overrideprotected voidOnDraw (canvas canvas) {//TODO auto-generated Method Stub Super. OnDraw (canvas); if(MBytes = =NULL) { return; } if(Mpoints = =NULL|| Mpoints.length < Mbytes.length * 4) {mpoints=New float[Mbytes.length * 4]; } mrect.set (0, 0, GetWidth (), getheight ()); for(inti = 0; i < mbytes.length-1; i++) {mpoints[i* 4] = mrect.width () * I/(mbytes.length-1); Mpoints[i* 4 + 1] = Mrect.height ()/2 + ((byte) (Mbytes[i] + +)) * (Mrect.height ()/2) /128; Mpoints[i* 4 + 2] = Mrect.width () * (i + 1) /(Mbytes.length-1); Mpoints[i* 4 + 3] = Mrect.height ()/2 + ((byte) (Mbytes[i + 1] + +)) * (Mrect.height ()/2) /128; } canvas.drawlines (Mpoints, mpaint); } }}
Android Implementation Bounce Spectrum DEMO