如何在Android中以编程方式打开内置的均衡器?

1aaf6o9v  于 2023-05-27  发布在  Android
关注(0)|答案(4)|浏览(260)

我已经看到许多应用程序在内置均衡器中打开系统(Google Play音乐,Spotify,三星股票音乐播放器)。直接,而不必自己从头开始写。这些应用程序是如何做到这一点的?我找不到解决办法。

} 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;
vsdwdz23

vsdwdz231#

启动默认均衡器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基本上也是这样做的,还没有检查其他的。
startActivityForResult()的必要性在文档中解释:
意图携带由播放器应用使用的多个额外内容,以将必要的信息片段传送到控制面板应用。
调用应用程序必须使用android.app.Activity#startActivityForResult(Intent, int)方法来启动控制面板,以便控制面板应用程序指示并使用其包名称来跟踪此特定应用程序的更改。

dy2hfwbg

dy2hfwbg2#

你需要intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mp.getAudioSessionId();),然后你需要,但你需要一个MediaPlayer,变量,你需要显示均衡器意图连同audioSession和getPackageName在同一意图effects.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, this.getPackageName());中,还有意图intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);与audioSession和包名称。首先显示intent,然后是audiosession和getpackagename,
然后在您需要的服务。只有在播放不止一首歌的情况下。在服务中需要以下代码,否则eqaulizer将无法始终工作。

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);
    }

然后你必须接收广播,让它一直工作,你需要做一个int audioSession = 0;
String getPackageName = getPackageName();
然后你需要,广播包名称maybe和audiosession,然后使用变量,而不是代码。均衡器显示意图、软件包名称和音频效果是意图显示均衡器工作所必需的,仅在“活动”中工作。
广播接收器我想尝试使用。班级,

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;

然后你需要从服务中广播包名,以活动计算它。用一个变量要求更高的话就用字符串,把pacakename广播,然后用变量更高的和在广播。然后把这个放进去,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"));

做了。希望它能起作用,让我知道我可能会回应,如果有什么丢失。抱歉有些代码格式不对,请大家看完后把所有代码放在一起,如果看到写的话,以后的代码请分开。

tvmytwxo

tvmytwxo3#

这是我在Android中打开内置均衡器的函数,你只需要在下面的函数中传递Activity Context并给予会话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();
        }
    }
}

上面的代码是为我工作,我希望它也会为你工作...

c0vxltue

c0vxltue4#

顺便说一句,均衡器将无法正常工作,如果从意图的请求代码,为ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL,有没有地方,去,要解决这个问题把这个代码在您的应用程序,或效果将无法工作在所有可能把这个代码,@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); } }谢谢,注意:他被允许使用他的请求代码,而不仅仅是我的0。

相关问题