我正在尝试为我的音乐播放器应用程序创建一个简单的均衡器,在AudioFxDemo之后,我在女巫中创建了一个简单的活动,我有 4 个不同的搜索栏,应该会改变某些乐队的音量。现在我在调用我的 EqualizerActivity 的 MainActivity 中实例化了我的 MediaPlayer 和均衡器,我将均衡器变量设置为公共静态,以便我可以将它放入我的 EqualizerActivity。
//MediaPlayer
mp = new MediaPlayer();
equalizer = new Equalizer(0, mp.getAudioSessionId());
int val = equalizer.setEnabled(true);
if(val != Equalizer.SUCCESS)
Log.v("A", "EQUALIZER NON ATTIVO");
setVolumeControlStream(AudioManager.STREAM_MUSIC);
我的问题是 equalizer.setEnabled(true) 返回 ERROE_INVALID_OPERATION (-5) 而不是 SUCCESS,此时我的代码可以正常工作,但我不能使用均衡器。我真的不知道问题出在哪里,但我认为可能是我将均衡器设置为静态,是另一种将均衡器传递给 EqualizerActivity 的方法吗?
public class EqualizerActivity extends Activity {
private Equalizer equalizer;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
equalizer = MainActivity.equalizer; //preso dall'activity -> sempre attivo
int val = equalizer.setEnabled(true);
if(val != Equalizer.SUCCESS)
Log.v("A", "EQUALIZER NON ATTIVO " + val);
setupEqualizerFXandUI();
}
//generato dinamicamente a seconda delle bande percepite
private void setupEqualizerFXandUI()
{
TextView eqTextView = new TextView(this);
eqTextView.setText("Equalizer:");
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(eqTextView);
setContentView(ll);
short bands = equalizer.getNumberOfBands();
final short minEQLevel = equalizer.getBandLevelRange()[0];
final short maxEQLevel = equalizer.getBandLevelRange()[1];
for(short i = 0; i< bands; i++)
{
final short band = i;
//Log.v("A", "B "+ band);
TextView freqTv = new TextView(this);
freqTv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
freqTv.setGravity(Gravity.CENTER_HORIZONTAL);
freqTv.setText((equalizer.getCenterFreq(band) /1000) + " Hz");
ll.addView(freqTv);
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
TextView minDbTv = new TextView(this);
minDbTv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
minDbTv.setText((minEQLevel / 100) + " dB");
TextView maxDbTv = new TextView(this);
maxDbTv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
maxDbTv.setText((maxEQLevel / 100) + " dB");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.weight = 1;
SeekBar bar = new SeekBar(this);
bar.setLayoutParams(layoutParams);
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(equalizer.getBandLevel(band)); // Volume della banda
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
equalizer.setBandLevel(band, (short)(progress + minEQLevel));
Log.v("A", "LEVEL: " + (progress + minEQLevel));
}
});
row.addView(minDbTv);
row.addView(bar);
row.addView(maxDbTv);
ll.addView(row);
}
}
}
在 MainActivity 我释放均衡器:
public void onDestroy()
{
super.onDestroy();
if(mNotify != null)
mNotify.cancel(001);
if(mHandler != null)
mHandler.removeCallbacks(mUpdateTimeTask); //rimuovo il thread che aggiorna la seekbar
if(mp != null){
mp.release(); //rilascio il media player
}
if(equalizer != null)
equalizer.release();
if(bassboost != null)
bassboost.release();
}