How to open the built in Equalizer programmatically in Android?
Asked Answered
S

4

9

I have seen many apps opening systems in built Equalizer (google play music, Spotify, Samsung stock music player). Directly, without having to write their own from scratch. How do these apps do that? I couldn't find a solution.

} else if (id == R.id.action_fx) {
        Intent intent = new Intent();
        intent.setAction("android.media.action.DISPLAY_AUDIO_EFFECT_CONTROL_PANEL");
        if ((intent.resolveActivity(getPackageManager()) != null)) {
            startActivity(intent);
        } else {
            Intent intent11 = new Intent(MainActivity.this, Help.class);
            intent11.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent11);
            // No equalizer found :(
        }
        return true;
Somatotype answered 12/6, 2016 at 13:12 Comment(0)
E
9

The following should work to start the default equalizer Activity:

Intent intent = new Intent(AudioEffect
    .ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);

if ((intent.resolveActivity(getPackageManager()) != null)) {
    startActivityForResult(intent, REQUEST_EQ);
} else {
    // No equalizer found :(
}

Spotify does the same basically, haven't checked the others.

The necessity of startActivityForResult() is explained in the docs:

The intent carries a number of extras used by the player application to communicate necessary pieces of information to the control panel application.

The calling application must use the android.app.Activity#startActivityForResult(Intent, int) method to launch the control panel so that its package name is indicated and used by the control panel application to keep track of changes for this particular application.

Emmalynn answered 12/6, 2016 at 14:0 Comment(0)
S
0

You need, intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mp.getAudioSessionId();) then you need, but you need a MediaPlayer, variable, you need the display equalizer intent together with the audioSession and getPackageName in the same intent effects.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, this.getPackageName()); also, Intent intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); with the audiosession and package name. display intent first, then the audiosession and getpackagename,

Then in the service you need. Only if more than one song is playing. You need the following code in the service, otherwise the eqaulizer won't always work.

public void openAudioFx() {
    Intent i = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
    i.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName);
    i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSession);
    this.sendBroadcast(i);
}

public void closeAudioFx() {
    Intent k = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
    k.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName);
    k.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audioSession);
    this.sendBroadcast(k);
    } 

then you have to receive the broadcast, to make it work all the time you need to make a an int audioSession = 0;

and String getPackageName = getPackageName();

Then you need, to broadcast package name maybe and audiosession, then use the variables, instead of code. The eqaulizer display intent, and package name and audioeffect, are necessary for the intent display equalizer to work, only works in an Activity.

The broadcast receiver I think try to use. the class,

public final BroadcastReceiver hello;
{
    hello = new BroadcastReceiver() {


        private static final String TAG = "mMessageReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String action = intent.getAction();


                switch (action) {
                    case AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION:
                        // Log.d(TAG_MAIN_ACTIVITY, action);


                        break;
                    case AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION:
                        break;
                    case AudioEffect.EXTRA_AUDIO_SESSION:
                        audioSession = intent.getIntExtra(AudioEffect.EXTRA_AUDIO_SESSION, 0);


                        break;
                    case AudioEffect.EXTRA_PACKAGE_NAME:
                        // Log.d(TAG_MAIN_ACTIVITY, action);


                        break;

then you need to broadcast the packagename from the service, to activity figure it out. with a variable required higher then somehow use string, to the pacakename broadcast, then use the variable higher and in the broadcast. Then put this in, onCreate public final BroadcastReceiver hello;

{
    hello = new BroadcastReceiver() {


        private static final String TAG = "mMessageReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String action = intent.getAction();


                switch (action) {
                    case AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION:
                        // Log.d(TAG_MAIN_ACTIVITY, action);
                        break;
                    case AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION:
                        break;
                    case AudioEffect.EXTRA_AUDIO_SESSION:
                        audioSession = intent.getIntExtra(AudioEffect.EXTRA_AUDIO_SESSION, 0);
                        break;
                    case AudioEffect.EXTRA_PACKAGE_NAME:
                        // Log.d(TAG_MAIN_ACTIVITY, action);
                        break;
                    }

    LocalBroadcastManager.getInstance(this).registerReceiver(hello, new IntentFilter("ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION"));
    LocalBroadcastManager.getInstance(this).registerReceiver(hello, new IntentFilter("ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION"));
    LocalBroadcastManager.getInstance(this).registerReceiver(hello, new IntentFilter("EXTRA_PACKAGE_NAME"));
    LocalBroadcastManager.getInstance(this).registerReceiver(hello, new IntentFilter("EXTRA_AUDIO_SESSION")); 

done. Hopefully it works, let me know I may respond if anything is missing. Sorry some of the code is not formatted properly, please read and put all the code together, then if you can see writing, after code, then please separate.

Solvable answered 8/2, 2019 at 5:50 Comment(0)
F
0

Here is the function I made for opening built-in Equalizer in Android, you just need to pass Activity Context in the below function and give the session ID.

    public static void openEqualizer(@NonNull final Activity activity) throws RemoteException {
    final int sessionId = MediaPlaybackServiceManager.sService.getAudioSessionId();
    if (sessionId == AudioEffect.ERROR_BAD_VALUE) {
        Toast.makeText(activity, "No Session Id", Toast.LENGTH_LONG).show();
    } else {
        try {
            final Intent effects = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
            effects.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, "your app package name");
            effects.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, sessionId);
            effects.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC);
            activity.startActivityForResult(effects, 0);
        } catch (@NonNull final ActivityNotFoundException notFound) {
            Toast.makeText(activity, "There is no equalizer", Toast.LENGTH_SHORT).show();
        }
    }
}

The above code is working for me and I hope it will work for you too...

Freehold answered 7/4, 2020 at 7:25 Comment(0)
R
-1

Btw, the equalizer will not work properly, if the request code from the intent, for ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL, has no where, to go to, to fix this issue put this code in your app, or effects won't work at all possibly put this code in, @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0 && resultCode == RESULT_OK) { final Intent effects = new Intent(); effects.putExtra(AudioEffect.EXTRA_PACKAGE_NAME,getPackageName()); effects.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, audiosession); effects.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC); effects.putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.EFFECT_TYPE_EQUALIZER); startActivity(effects); } } thanks, note: he is allowed to use his request code, not just my 0.

Remodel answered 22/5, 2023 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.