你检查过AudioFXDemo的 android 示例代码吗?如果没有,请检查它,看看它是否对您有帮助。
有与谷歌音乐应用程序相同的均衡器。
检查该演示示例中的以下代码:
private void setupEqualizerFxAndUI() {
// Create the Equalizer object (an AudioEffect subclass) and attach it to our media player,
// with a default priority (0).
try {
System.out.println("setupEqualizerFxAndUI eEqualizer is: "+mEqualizer);
System.out.println("setupEqualizerFxAndUI mIRemoteService: "+mIRemoteService);
mEqualizer = new Equalizer(0,mIRemoteService.getAudioSessionId());
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} // Error in this line
if(onOffBtn.isChecked()){
mEqualizer.setEnabled(true);
}
short bands = 5 ;
//System.out.println("bands are: "+bands);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1;
LinearLayout newOne = new LinearLayout(this);
newOne.setLayoutParams(params);
newOne.setOrientation(LinearLayout.HORIZONTAL);
final short minEQLevel = mEqualizer.getBandLevelRange()[0];
final short maxEQLevel = mEqualizer.getBandLevelRange()[1];
//System.out.println("Minimum value::: "+minEQLevel);
//System.out.println("Maximum value::: "+maxEQLevel);
//VerticalSeekBar[] bar = new VerticalSeekBar[5];
for (short i = 0; i < bands; i++) {
final short band = i;
/* TextView freqTextView = new TextView(this);
freqTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
freqTextView.setGravity(Gravity.CENTER_HORIZONTAL);
freqTextView.setText((mEqualizer.getCenterFreq(band) / 1000) + " Hz");
newOne.addView(freqTextView);*/
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.VERTICAL);
TextView minDbTextView = new TextView(this);
minDbTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
minDbTextView.setText((minEQLevel / 100) + " dB");
minDbTextView.setTextColor(0xff000000);
TextView maxDbTextView = new TextView(this);
maxDbTextView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
maxDbTextView.setText((maxEQLevel / 100) + " dB");
maxDbTextView.setTextColor(0xff000000);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 300);
layoutParams.setMargins(0, 10, 0, 10);
//--------------------------
// setClassicTone(maxEQLevel, minEQLevel);
VerticalSeekBar bar = new VerticalSeekBar(this);
bar = new VerticalSeekBar(this);
bar.setLayoutParams(layoutParams);
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(mEqualizer.getBandLevel(band));
//bar.setMax(3000);
//bar.setProgress(mEqualizer.getBandLevel(band)+1500);
bar.setPadding(0, 10, 0, 10);
//bar.setProgressDrawable(R.drawable.scrubber_progress_horizontal_holo_light);
bar.setProgressDrawable(getResources().getDrawable(R.drawable.scrubber_progress_horizontal_holo_light));
bar.setThumb(getResources().getDrawable(R.drawable.scrubber_control_selector_holo));
//System.out.println("Progress:::"+(mEqualizer.getBandLevel(band)));
//bar[i].setProgress((maxEQLevel-minEQLevel)/2);
mEqualizer.setBandLevel(band,(short) ((maxEQLevel-minEQLevel)));
//System.out.println("Presets are: "+mEqualizer.getNumberOfPresets()+" And name is: "+mEqualizer.getPresetName(i));
//mEqualizer.setBandLevel(band, level)
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
System.out.println("Seek bar change ");
presetsSpinner.setSelection(0);
mEqualizer.setBandLevel(band, (short) (progress + minEQLevel));
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
row.addView(maxDbTextView);
row.addView(bar);
row.addView(minDbTextView);
newOne.addView(row, params);
}
equalizerLayout.addView(newOne);
}
VerticalSeekBar 是如下类:
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class VerticalSeekBar extends SeekBar {
public VerticalSeekBar(Context context) {
super(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
希望以上代码对您有所帮助。如果没有,请告诉我。
享受编码... :)