如何在 Android 中以编程方式打开内置均衡器?
How to open the built in Equalizer programmatically in Android?
我看到很多应用程序在内置均衡器中打开系统(google 播放音乐、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;
以下应该可以启动默认均衡器 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()
的必要性在 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.
你需要,intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mp.getAudioSessionId();)
那么你需要,但你需要一个 MediaPlayer,变量,你需要显示均衡器意图以及同一意图中的 audioSession 和 getPackageName effects.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, this.getPackageName());
此外,Intent intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
带有音频会话和包名称。首先显示意图,然后是音频会话和 getpackagename,
然后在你需要的服务中。仅当播放不止一首歌曲时。您需要在服务中添加以下代码,否则均衡器将无法始终正常工作。
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();
那么你需要广播包名和音频会话,然后使用变量,而不是代码。均衡器显示意图,以及包名和音频效果,是意图显示均衡器工作所必需的,仅在Activity.
下有效
我想尝试使用的广播接收器。 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;
然后您需要从服务广播包名,activity 弄清楚。需要更高的变量然后以某种方式使用字符串,对 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"));
完成。希望它有效,让我知道如果有任何遗漏,我可能会回复。抱歉,部分代码格式不正确,请阅读并将所有代码放在一起,然后如果您能看到写的代码,请在代码之后分开。
这是我在Android中打开内置均衡器的函数,你只需要在下面的函数中传递Activity上下文并给出会话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();
}
}
}
上面的代码对我有用,我希望它对你也有用...
我看到很多应用程序在内置均衡器中打开系统(google 播放音乐、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;
以下应该可以启动默认均衡器 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()
的必要性在 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.
你需要,intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, mp.getAudioSessionId();)
那么你需要,但你需要一个 MediaPlayer,变量,你需要显示均衡器意图以及同一意图中的 audioSession 和 getPackageName effects.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, this.getPackageName());
此外,Intent intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
带有音频会话和包名称。首先显示意图,然后是音频会话和 getpackagename,
然后在你需要的服务中。仅当播放不止一首歌曲时。您需要在服务中添加以下代码,否则均衡器将无法始终正常工作。
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();
那么你需要广播包名和音频会话,然后使用变量,而不是代码。均衡器显示意图,以及包名和音频效果,是意图显示均衡器工作所必需的,仅在Activity.
下有效我想尝试使用的广播接收器。 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;
然后您需要从服务广播包名,activity 弄清楚。需要更高的变量然后以某种方式使用字符串,对 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"));
完成。希望它有效,让我知道如果有任何遗漏,我可能会回复。抱歉,部分代码格式不正确,请阅读并将所有代码放在一起,然后如果您能看到写的代码,请在代码之后分开。
这是我在Android中打开内置均衡器的函数,你只需要在下面的函数中传递Activity上下文并给出会话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();
}
}
}
上面的代码对我有用,我希望它对你也有用...