我想創造我的音樂播放器應用一個簡單的均衡器,下面我創建了一個魔女一個簡單的活動AudioFxDemo我一直認爲應該改變一些帶的卷4個不同seekbars 。 現在我有我的MediaPlayer和均衡器在調用我的EqualizerActivity在MainActivity實例,我已經設置均衡器變量作爲公共靜態,這樣我可以把它變成我的EqualizerActivity。的setEnabled均衡器返回無效操作錯誤
//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(真)返回ERROE_INVALID_OPERATION(-5),而不是成功,在這一點上我已經得到了代碼工作,但我不能使用均衡器。 我真的不知道是什麼問題,但我認爲這可能是我將均衡器設置爲靜態,是將均衡器傳遞給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();
}
你在做MainActivity中的其他均衡器操作嗎? – Sudhee 2013-05-05 12:22:26
此外,請確認此行爲是否在設備或模擬器上?如果在設備上,模擬器上的行爲是什麼? – Sudhee 2013-05-05 12:37:20
在我的設備(galaxy S2)上進行測試,只是在模擬器上試過,我仍然有錯誤。我用equalizer做的唯一其他事情是將其聲明爲公共靜態均衡器,並在關閉應用程序時將其釋放。我真的搞不清楚 – Pievis 2013-05-05 13:00:05