如何让我的Android应用出现在音频输出建议及附加音频推荐列表中?
Hey there! Let's walk through how to get your Android app showing up in two key audio-related lists—system audio output suggestions and the app picker for attaching audio. I've implemented both before, so here's a practical, step-by-step guide:
To make your app appear as an option in the system's audio output picker (like the one you see when tapping the audio icon in the status bar), you'll need to use Android's MediaRouter framework to register your app as a valid audio route target.
Step 1: Add Permissions & Dependencies
First, add the necessary permissions to yourAndroidManifest.xml(skip the internet permission if your app doesn't stream audio over network):<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />Make sure your project includes the MediaRouter AndroidX library (most modern projects have this by default, but add it to your
build.gradleif not):implementation 'androidx.mediarouter:mediarouter:1.3.0'Step 2: Create a Custom MediaRouteProvider Service
This service tells the system your app can act as an audio output destination. Create a class extendingMediaRouteProvider:public class MyAudioRouteProvider extends MediaRouteProvider { public MyAudioRouteProvider(Context context) { super(context); } @Override public RouteController onCreateRouteController(String routeId) { // Handle connection/disconnection logic for your audio route here return new MyRouteController(); } @Override public void onDiscoveryRequestChanged(MediaRouteDiscoveryRequest request) { // Publish your app's route info to the system MediaRouteDescriptor routeDescriptor = new MediaRouteDescriptor.Builder( "my_app_audio_route", "My App Audio Output") .setDescription("Stream audio to My App") .setIconDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_audio_output)) .setConnectionState(MediaRouter.RouteInfo.CONNECTION_STATE_DISCONNECTED) .setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE) .setPlaystreamType(MediaRouter.RouteInfo.PLAYSTREAM_TYPE_MUSIC) .build(); MediaRouteProviderDescriptor providerDescriptor = new MediaRouteProviderDescriptor.Builder() .addRoute(routeDescriptor) .build(); setDescriptor(providerDescriptor); } private class MyRouteController extends RouteController { @Override public void onConnect() { // Logic to start receiving audio stream super.onConnect(); } @Override public void onDisconnect() { // Logic to stop receiving audio stream super.onDisconnect(); } } }Step 3: Register the Service in Manifest
Add the service to yourAndroidManifest.xmlso the system can discover it:<service android:name=".MyAudioRouteProvider" android:exported="true"> <intent-filter> <action android:name="android.media.MediaRouteProviderService" /> </intent-filter> </service>Note: Test this by opening the system audio output picker—your app should now appear as an option.
When users tap an "Attach Audio" button in other apps (like notes or social apps), your app should show up alongside the system recorder. To do this, you need to make your app respond to audio capture/selection intents.
Step 1: Add Intent Filters to Your Activity
In yourAndroidManifest.xml, add intent filters to the activity that handles audio capture/selection. This tells the system your app can handle audio-related requests:<activity android:name=".AudioCaptureActivity" android:label="My Audio Tool" android:icon="@drawable.ic_microphone"> <!-- Respond to audio recording requests --> <intent-filter> <action android:name="android.media.action.RECORD_SOUND" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <!-- Respond to audio file selection requests (optional) --> <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <category android:name="android.intent.category.OPENABLE" /> <data android:mimeType="audio/*" /> </intent-filter> </activity>Step 2: Handle the Intent & Return Results
In yourAudioCaptureActivity, process the incoming intent, handle audio capture/selection, and return the result to the calling app:public class AudioCaptureActivity extends AppCompatActivity { private static final int REQUEST_AUDIO_ACTION = 1; private Uri audioUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_audio_capture); Intent incomingIntent = getIntent(); if (android.media.action.RECORD_SOUND.equals(incomingIntent.getAction())) { // Launch system recorder or your custom recording flow Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); startActivityForResult(recordIntent, REQUEST_AUDIO_ACTION); } else if (Intent.ACTION_GET_CONTENT.equals(incomingIntent.getAction())) { // Launch file picker to select existing audio Intent pickIntent = new Intent(Intent.ACTION_GET_CONTENT); pickIntent.setType("audio/*"); startActivityForResult(pickIntent, REQUEST_AUDIO_ACTION); } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_AUDIO_ACTION && resultCode == RESULT_OK) { if (data != null && data.getData() != null) { audioUri = data.getData(); // Send the audio URI back to the calling app Intent resultIntent = new Intent(); resultIntent.setData(audioUri); setResult(RESULT_OK, resultIntent); finish(); } } } }Step 3: Polish the Picker Appearance
Theandroid:labelandandroid:iconattributes you added to the activity will control how your app looks in the picker—make sure they're descriptive and match your app's branding.
Now test this by opening an app with an "Attach Audio" button (like Google Keep) and tapping it—your app should appear alongside the system recorder in the app selection list.
内容的提问来源于stack exchange,提问作者Rahul Gaba

