Home Register as music player and play song
Reply: 5

Register as music player and play song

Yaroslav Mytkalyk
1#
Yaroslav Mytkalyk Published in 2016-11-01 14:25:02Z

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.

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>
Yaroslav Mytkalyk
2#
Yaroslav Mytkalyk Reply to 2016-11-07 12:05:08Z

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

Jerome
3#
Jerome Reply to 2016-11-09 13:45:03Z

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)
        {   ...
        }
    }

}

Vijay Kumar M
4#
Vijay Kumar M Reply to 2016-11-09 18:00:52Z

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.

Lucien Mendela
5#
Lucien Mendela Reply to 2016-11-09 21:05:49Z

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>
Zhao Lin
6#
Zhao Lin Reply to 2016-11-10 01:48:45Z
        <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>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.327421 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO