android均衡器无法正常工作

时间:2017-06-11 18:54:41

标签: android android-mediaplayer equalizer

我正在关注this 教程,除了没有创建搜索栏和微调器之外,每个东西都正常工作。 这就是我想要的 的java:

public class MainActivity extends AppCompatActivity {

private MediaPlayer mediaPlayer;
private Equalizer equalizer;
private LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mediaPlayer = MediaPlayer.create(this, R.raw.song);
    mediaPlayer.start();
    equalizer = new Equalizer(0, mediaPlayer.getAudioSessionId());
    equalizer.setEnabled(true);
    setUpEqualizer();

    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {

        }
    });
}

public void setUpEqualizer() {
    linearLayout = (LinearLayout) findViewById(R.id.linearlayequ);
    TextView equheading = new TextView(this);
    equheading.setText("Equalizer");
    equheading.setTextSize(20);
    equheading.setGravity(Gravity.CENTER_HORIZONTAL);
    linearLayout.addView(equheading);
    short nofrequencyband = equalizer.getNumberOfBands();
    final short lowerfrequencyband = equalizer.getBandLevelRange()[0];
    final short upperfrequencyband = equalizer.getBandLevelRange()[1];
    for (short i = 0; i < nofrequencyband; i++) {
        final short eqbandindex = i;
        TextView frheadertv = new TextView(this);
        frheadertv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        frheadertv.setGravity(Gravity.CENTER_HORIZONTAL);

        frheadertv.setText((equalizer.getCenterFreq(eqbandindex) / 1000) + "Hz");
        linearLayout.addView(frheadertv);
        LinearLayout seeklay = new LinearLayout(this);
        seeklay.setOrientation(LinearLayout.HORIZONTAL);

        TextView lowereqbandlvl = new TextView(this);
        lowereqbandlvl.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        lowereqbandlvl.setText((lowerfrequencyband / 100) + "dB");

        TextView highreqbandlvl = new TextView(this);
        highreqbandlvl.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        highreqbandlvl.setText((upperfrequencyband / 100) + "dB");

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.weight = 1;
        SeekBar seekBar = new SeekBar(this);
        seekBar.setId(i);
        seekBar.setLayoutParams(layoutParams);
        seekBar.setMax(upperfrequencyband - lowerfrequencyband);
        seekBar.setProgress(equalizer.getBandLevel(eqbandindex));
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                equalizer.setBandLevel(eqbandindex, (short) (i + lowerfrequencyband));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seeklay.addView(lowereqbandlvl);
        seeklay.addView(seekBar);
        seeklay.addView(highreqbandlvl);
        linearLayout.addView(seeklay);
        equalizSound();
    }
}

public void equalizSound() {
    ArrayList<String> equalizername = new ArrayList<String>();
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner spinner = (Spinner)findViewById(R.id.spinner);
    for (short i=0;i<equalizer.getNumberOfPresets();i++){
        equalizername.add(equalizer.getPresetName(i));
    }
    spinner.setAdapter(spinnerAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        }

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

        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing() && mediaPlayer != null) {

        equalizer.release();
        mediaPlayer.release();
        mediaPlayer = null;
    }
}
}

使用均衡器是否需要任何权限,或者我犯了错误,任何建议都会有所帮助。应用程序不会抛出任何异常,只会播放该歌曲。

1 个答案:

答案 0 :(得分:0)

我用它来制作均衡器

public class MainActivity extends AppCompatActivity
    implements SeekBar.OnSeekBarChangeListener,
    CompoundButton.OnCheckedChangeListener {
TextView bass_boost_label = null;
SeekBar bass_boost = null;
CheckBox enabled = null;
Button flat = null;
Equalizer eq = null;
BassBoost bb = null;
Virtualizer vr = null;
SeekBar virtal = null;
int min_level = 0;
int max_level = 100;

static final int MAX_SLIDERS = 5; // Must match the XML layout
SeekBar sliders[] = new SeekBar[MAX_SLIDERS];
TextView slider_labels[] = new TextView[MAX_SLIDERS];
public static int num_sliders = 0;
private MediaPlayer mediaPlayer;
private ArrayList<String> equalizername = new ArrayList<String>();
private Spinner spinner;


/*=============================================================================
    onCreate
=============================================================================*/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    enabled = (CheckBox) findViewById(R.id.enabled);
    enabled.setOnCheckedChangeListener(this);


    bass_boost = (SeekBar) findViewById(R.id.bass_boost);
    bass_boost.setOnSeekBarChangeListener(this);
    bass_boost_label = (TextView) findViewById(R.id.bass_boost_label);

    sliders[0] = (SeekBar) findViewById(R.id.slider_1);
    slider_labels[0] = (TextView) findViewById(R.id.textView);
    sliders[1] = (SeekBar) findViewById(R.id.slider_2);
    slider_labels[1] = (TextView) findViewById(R.id.textView2);
    sliders[2] = (SeekBar) findViewById(R.id.slider_3);
    slider_labels[2] = (TextView) findViewById(R.id.textView3);
    sliders[3] = (SeekBar) findViewById(R.id.slider_4);
    slider_labels[3] = (TextView) findViewById(R.id.textView4);
    sliders[4] = (SeekBar) findViewById(R.id.slider_5);
    slider_labels[4] = (TextView) findViewById(R.id.textView5);
    virtal = (SeekBar) findViewById(R.id.seekBar2);

    mediaPlayer = MediaPlayer.create(this, R.raw.song2);
    mediaPlayer.start();
    eq = new Equalizer(0, mediaPlayer.getAudioSessionId());
    for (short i = 0; i < eq.getNumberOfPresets(); i++) {
        equalizername.add(eq.getPresetName(i));
        Log.e("name", "" + eq.getPresetName(i));
    }
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, equalizername);
    spinner.setAdapter(arrayAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            eq.usePreset((short) i);
            updateUI();
        }

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

        }
    });
    if (eq != null) {
        eq.setEnabled(true);
        int num_bands = eq.getNumberOfBands();
        Log.e("band", "" + num_bands);
        num_sliders = num_bands;
        short r[] = eq.getBandLevelRange();
        min_level = r[0];
        max_level = r[1];
        for (int i = 0; i < num_sliders && i < MAX_SLIDERS; i++) {
            int[] freq_range = eq.getBandFreqRange((short) i);
            sliders[i].setOnSeekBarChangeListener(this);
            slider_labels[i].setText(formatBandLabel(freq_range));
        }
    }
    for (int i = num_sliders; i < MAX_SLIDERS; i++) {
        sliders[i].setVisibility(View.GONE);
        slider_labels[i].setVisibility(View.GONE);
    }

    bb = new BassBoost(0, mediaPlayer.getAudioSessionId());
    if (bb != null) {
    } else {
        bass_boost.setVisibility(View.GONE);
        bass_boost_label.setVisibility(View.GONE);
    }
    vr = new Virtualizer(0, mediaPlayer.getAudioSessionId());
    virtal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            if (seekBar == virtal) {
                vr.setEnabled(level > 0 ? true : false);
                vr.setStrength((short) i); // Already in the right range 0-1000
            } else if (eq != null) {
                int new_level = min_level + (max_level - min_level) * level / 100;

                for (int j = 0; j < num_sliders; j++) {
                    if (sliders[j] == seekBar) {
                        eq.setBandLevel((short) j, (short) new_level);
                        break;
                    }
                }
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
    updateUI();
}

/*=============================================================================
    onProgressChanged
=============================================================================*/
@Override
public void onProgressChanged(SeekBar seekBar, int level,
                              boolean fromTouch) {
    if (seekBar == bass_boost) {
        bb.setEnabled(level > 0 ? true : false);
        bb.setStrength((short) level); // Already in the right range 0-1000
    } else if (eq != null) {
        int new_level = min_level + (max_level - min_level) * level / 100;

        for (int i = 0; i < num_sliders; i++) {
            if (sliders[i] == seekBar) {
                eq.setBandLevel((short) i, (short) new_level);
                break;
            }
        }
    }
}

/*=============================================================================
    onStartTrackingTouch
=============================================================================*/
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

/*=============================================================================
    onStopTrackingTouch
=============================================================================*/
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

/*=============================================================================
    formatBandLabel
=============================================================================*/
public String formatBandLabel(int[] band) {
    return milliHzToString(band[0]);
    /**
     change
     /*/
}

/*=============================================================================
    milliHzToString
=============================================================================*/
public String milliHzToString(int milliHz) {
    if (milliHz < 1000) return "";
    if (milliHz < 1000000)
        return "" + (milliHz / 1000);
    else
        return "" + (milliHz / 1000000) + "k";
}


/*=============================================================================
    updateSliders
=============================================================================*/
public void updateSliders() {
    for (int i = 0; i < num_sliders; i++) {
        int level;
        if (eq != null)
            level = eq.getBandLevel((short) i);
        else
            level = 0;
        int pos = 100 * level / (max_level - min_level) + 50;
        sliders[i].setProgress(pos);
    }
}

/*=============================================================================
    updateBassBoost
=============================================================================*/
public void updateBassBoost() {
    if (bb != null)
        bass_boost.setProgress(bb.getRoundedStrength());
    else
        bass_boost.setProgress(0);
}

/*=============================================================================
    onCheckedChange
=============================================================================*/
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
    if (view == (View) enabled) {
        eq.setEnabled(isChecked);
        bb.setEnabled(true);
        vr.setEnabled(true);
    }
}


/*=============================================================================
    updateUI
=============================================================================*/
public void updateUI() {
    updateSliders();
    updateBassBoost();
    enabled.setChecked(eq.getEnabled());
}

/*=============================================================================
    setFlat
=============================================================================*/
public void setFlat() {
    if (eq != null) {
        for (int i = 0; i < num_sliders; i++) {
            eq.setBandLevel((short) i, (short) 0);
        }
    }

    if (bb != null) {
        bb.setEnabled(false);
        bb.setStrength((short) 0);
    }

    updateUI();
}

/*=============================================================================
    showAbout


/*=============================================================================
    onOptionsItemSelected
=============================================================================*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return super.onOptionsItemSelected(item);
}

}

希望它会对某人有所帮助。