I'm trying to create a simple equalizer for my music player application, following the AudioFxDemo I've created a simple activity in witch I've 4 different seekbars that should change the volumes of some bands. For now I have my MediaPlayer and Equalizer instantiated in the MainActivity that calls my EqualizerActivity, I've set the equalizer variable as a public static so that I can get it into my 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);
My problem is that the equalizer.setEnabled(true) returns ERROE_INVALID_OPERATION (-5) instead of SUCCESS, at this point I've got the code working but I can't use the equalizer. I really don't know what's the problem, but I think it could be that I'm setting the equalizer as static, is the another way to pass the equalizer to the 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);
}
}
}
In the MainActivity I release the equalizer:
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();
}
Ok, seems like that n7player (a music application I'm using) had a process that took the equalizer with the highest priority (I think). Closing the n7player process I'm able to use the equalizer and the bass booster.