2

Im looping all my presets and want to use them after that. Now the first getBandLevel method Im calling is returning me an acceptable value but all the other method calls in the loop returns me 0. Where could I be wrong ?

Here is my code with the loop:

m_equalizer.usePreset((short)position);
short numberFrequencyBands = .m_equalizer.getNumberOfBands();
final short lowerEqualizerBandLevel = m_equalizer.getBandLevelRange()[0];

for(short x=0;x<numberFrequencyBands;x++)
{
    SeekBar seekbar= (SeekBar)findViewById((short)x);
    seekbar.setProgress(m_equalizer.getBandLevel((short) x) - lowerEqualizerBandLevel ); //Here is getBandLevel returning me 0 after x is 1
}

Edit: I also checked if other apps are also giving 0 and they dont.They have right settings

Code:

public void equalizeSound()
{
   ArrayList<String> equalizePresetNames = new ArrayList<String>();
    ArrayAdapter<String> equalizerPresetSpinnerAdapter = new ArrayAdapter<String>(this,R.layout.custom_spinner_item_small,equalizePresetNames);
    equalizerPresetSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner equalizerPresetSpinner = (Spinner)findViewById(R.id.spinner_presets_eq);

    for(short i=0;i< m_equalizer.getNumberOfPresets();++i)
    {
        equalizePresetNames.add(m_equalizer.getPresetName(i));
    }

    equalizerPresetSpinner.setAdapter(equalizerPresetSpinnerAdapter);

    equalizerPresetSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            m_equalizer.usePreset((short)position);
            short numberFrequencyBands = m_equalizer.getNumberOfBands();
            final short lowerEqualizerBandLevel = m_equalizer.getBandLevelRange()[0];

            for(short x=0;x<numberFrequencyBands;x++)
            {
                SeekBar seekbar= (SeekBar)findViewById((short)x);
                seekbar.setProgress(m_equalizer.getBandLevel((short) x) - lowerEqualizerBandLevel );
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}
Ahmet K
  • 713
  • 18
  • 42
  • How are you initializing your `Equalizer` object? What is the value of `position`? – antonio Mar 24 '16 at 11:47
  • Thanks for your help. Im initalizing it like this : m_equalizer = new Equalizer(0, mplayer.getAudioSessionId()); . Position holds the value which Item of the Spinner is clicked. So it is an index of a preset in my Spinner – Ahmet K Mar 24 '16 at 15:32
  • 1
    So are you querying `getNumberOfPresets` and `getPresetName` to populate your `Spinner`? Could you post this part of your code and also the listeners of your `Spinner`? – antonio Mar 24 '16 at 15:55
  • I did edited my first post. This function is being called after the UI is set up – Ahmet K Mar 24 '16 at 15:59
  • I have tested your code and it's returning correct values. The only difference is that I'm initializing my equalizer doing `Equalizer m_equalizer = new Equalizer(0,0);` (of course "Flat" is returning all zero values and "Normal" is returning 300, 0, 0, 0, 300) – antonio Mar 24 '16 at 16:18
  • Have your tried logging the values that you get or are you relying on your `SeekBar`s? The line `SeekBar seekbar= (SeekBar)findViewById((short)x);` seems very suspicious. Why do you think that you will get the correct `Seekbar` querying `findViewById` with the `x` variable of your `for` loop? – antonio Mar 24 '16 at 16:26
  • Because Im creating the UI (the Seekbars) programmatically with a Loop and they have the id from 0-n .The reason why I did it programmitically is because every device support different numbers of Bands. What do you mean with logging the values? Im not getting any value from these presets. Im very thankful that youre helping me :) – Ahmet K Mar 24 '16 at 16:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107252/discussion-between-antonio-and-ahmet-kazaman). – antonio Mar 24 '16 at 16:33

1 Answers1

2

The SeekBar.OnSeekBarChangeListener of your SeekBars is changing the values of the presets.

Set the OnSeekBarChangeListener inside the for loop, removing the previous one:

SeekBar seekbar= (SeekBar)findViewById((short)x);
seekbar.setOnSeekBarChangeListener(null);
seekbar.setProgress(m_equalizer.getBandLevel((short) x) - lowerEqualizerBandLevel );
seekbar.setOnSeekBarChangeListener(yourListenerHere);
antonio
  • 18,044
  • 4
  • 45
  • 61