Search code examples
androidandroid-mediaplayeraudiotrackaudioeffect

How to inherit all systemwide (native) audio effects into a mediaplayer app?


How to inherit and enable (make effectual) current audioeffects (bassboost, equalizer, whatever) from an app playing audiostreams, assuming some other app is at disposal on the device for the manipulation of the native intonation algorithms.

The API documentation is not obvious on how to achieve this.

The following is not working, or is insufficient:

Equalizer equalizer = new Equalizer(0, mediaPlayer.getAudioSessionId());
                        equalizer.setEnabled(true);

                        mediaPlayer.prepare();
                        mediaPlayer.start();

Solution

  • I can answer myself. The obvious solution should have been something like:

    Equalizer.inheritSystemEq()
    

    I got it to work only the following cumbersome way. Do note that the solution is deprecated, but that the deprecated solution has not been replaced by an alternative API.

    Equalizer equalizerSystem = new Equalizer(0, 0) //0 as second parameter is deprecated. 
    //get current eq settings for global mix out
    short band0 = equalizerSystem.getBandLevel((short) 0);
    short band1 = equalizerSystem.getBandLevel((short) 1);
    short band2 = equalizerSystem.getBandLevel((short) 2);
    short band3 = equalizerSystem.getBandLevel((short) 3);
    short band4 = equalizerSystem.getBandLevel((short) 4);
    
    equalizerSystem.release();
    
    //copy these "system" values (Set by another equalizer app) into
    //another equalizer instance particular for this audio session.
    
    Equalizer equalizer = new Equalizer(0, mediaPlayer.getAudioSessionId());
    equalizer.setEnabled(true);
    equalizer.setBandLevel((short) 0, band0);
    equalizer.setBandLevel((short) 1, band1);
    equalizer.setBandLevel((short) 2, band2);
    equalizer.setBandLevel((short) 3, band3);
    equalizer.setBandLevel((short) 4, band4);
    

    Finally, i.e. subsequently, call mediaPlayer.prepare() or mediaPlayer.prepareAsync, else the equalizerSystem = new Equalizer(0,0) will not work.

    Also set in AndroidManifest:

    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
    

    To adopt recurring changes to eq settings, there is additionally a need to detect this via OnParameterChangeListener.