Register as music player and play song

I’m creating a music player app and I want to register the app as a music player. So that when a user clicks on any song to play, my app should be suggested as one of the music players. I followed this guide .

After I added the necessary code in manifest.xml, it only shows my app in suggestions. When I select my app, nothing is played. I think there should be something like receiver in main activity to receive the media path to play the selected song. But I don’t know how to implement this.

  • “Conversion to Dalvik format failed with error 1” on external JAR
  • How can I create a multilingual android application?
  • Android Studio Keyboard Shortcut for full screen mode
  • Integrate an Android app (.apk) into an existing Android project
  • Setting Android Background & Persistence Menu Bar - Using attribute on older versions causes crash - Is there a theme /pattern approach?
  • Embedding ZXing in android app
  • I’ve added following code to my manifest.xml (for reference):

    <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="file"/>
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http" />
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </intent-filter>
        <intent-filter
            android:priority="-1">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="content" />
            <data android:mimeType="audio/*"/>
            <data android:mimeType="application/ogg"/>
            <data android:mimeType="application/x-ogg"/>
            <data android:mimeType="application/itunes"/>
        </intent-filter>
    

  • Prevent multiple copies of an Android service
  • Why is LogCat showing all items as warnings (orange)?
  • Android - How to change fragments in the Navigation Drawer
  • Best way to join tables using sqlite in android
  • How to draw waveform of Android's music player?
  • Calling native method twice of third party library in an Activity causes the Android application to close down
  • 5 Solutions collect form web for “Register as music player and play song”

    Once you added IntentFilters use getIntent() to get the data from it

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent(getIntent());
    }
    
    @Override
    protected void onNewIntent(final Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        handleIntent(intent);
    }
    
    private void handleIntent(@NonNull final Intent intent) {
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            try {
                IntentHandler.onActionView(intent);
            } catch (IOException e) {
                // Handle error
            }
        }
    }
    

    IntentHandler.java

    final class IntentHandler {
    
        private static final String TAG = "IntentHandler";
    
        private IntentHandler() {
    
        }
    
        static void onActionView(@NonNull final Intent intent) throws IOException {
            final Uri data = intent.getData();
            if (data == null) {
                Log.w(TAG, "Intent data is null");
                throw new IOException("Intent data is null");
            }
    
            final String scheme = data.getScheme();
            if (scheme == null) {
                Log.w(TAG, "Uri scheme is null");
                throw new IOException("Uri scheme is null");
            }
            switch (scheme) {
                case "file":
                    playFile(data);
                    break;
    
                case "content":
                    playContent(data);
                    break;
    
                case "http":
                    playHttp(data);
                    break;
    
                default:
                    Log.w(TAG, "Unhandled Uri scheme: " + scheme);
                    throw new IOException("Unhandled Uri scheme: " + scheme);
            }
        }
    
        private static void playFile(@NonNull final Uri data) throws IOException {
            Log.d(TAG, "playFile() path = " + data.getPath());
            // Play a plain file
        }
    
        private static void playContent(@NonNull final Uri data) throws IOException {
            Log.d(TAG, "playContent() " + data);
            // Use ContentResovler to query the Uri
        }
    
        private static void playHttp(@NonNull final Uri data) throws IOException {
            Log.d(TAG, "playHttp() " + data);
            // Play from network
        }
    }
    

    If you don’t support playing from ContentProvider or from network remove IntentFilter for “http” or “content” scheme accordingly from AndroidManifest.xml

    Did you try these intent filters ?

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="file"/>
        <data android:scheme="content"/>
        <data android:host="*"/>
        <data android:mimeType="audio/*"/>
        <data android:mimeType="application/ogg"/>
        <data android:mimeType="application/x-ogg"/>
        ...
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="file"/>
        <data android:scheme="content"/>
        <data android:host="*"/>
        <data android:pathPattern=".*\.mp3"/>
        <data android:pathPattern=".*\.ogg"/>
        <data android:pathPattern=".*\.wav"/>
        <data android:pathPattern=".*\.wma"/>
        <data android:pathPattern=".*\.flac"/>
        ...
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="audio/*"/>
    </intent-filter>
    

    Then in your activity :

    @Override
    public void onCreate(Bundle savedInstanceState)
    {   super.onCreate(savedInstanceState);
    
        ...
    
        Intent i = getIntent();
        Uri u = i.getData();
        String a = i.getAction();
    
        if ((u != null) && (a != null) && (a.equalsIgnoreCase("android.intent.action.VIEW")))
        {   String filename = null;
    
            if (i.getScheme().equalsIgnoreCase("file"))
            {   filename = u.getPath();
            }
            else if (i.getScheme().equalsIgnoreCase("content"))
            {   try
                {   String[] filepathcolumn = {MediaStore.Audio.Media.DATA};
                    Cursor cursor1 = getContentResolver().query(u, filepathcolumn, null, null, null);
                    cursor1.moveToFirst();
                    int columnindex1 = cursor1.getColumnIndex(filepathcolumn[0]);
                    filename = cursor1.getString(columnindex1);
                    cursor1.close();
                }
                catch (Exception e)
                {   e.printStackTrace();
                }
            }
    
            if (filename != null)
            {   ...
            }
        }
    

    }

    I think you only define the default music player when song is selected
    in Manifest file and also write the code for service which run in the
    Main Thread to play the song.

    Try using the code below

     <intent-filter>
                    <action android:name="android.intent.action.VIEW"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                    <data android:scheme="file" android:mimeType="audio/*" />
         </intent-filter>
    
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file"/>
                <data android:mimeType="audio/*"/>
                <data android:mimeType="application/ogg"/>
                <data android:mimeType="application/x-ogg"/>
                <data android:mimeType="application/itunes"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="http" />
                <data android:mimeType="audio/*"/>
                <data android:mimeType="application/ogg"/>
                <data android:mimeType="application/x-ogg"/>
                <data android:mimeType="application/itunes"/>
            </intent-filter>
            <intent-filter
                android:priority="-1">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="content" />
                <data android:mimeType="audio/*"/>
                <data android:mimeType="application/ogg"/>
                <data android:mimeType="application/x-ogg"/>
                <data android:mimeType="application/itunes"/>
            </intent-filter>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.