register as music player

后端 未结 4 974
星月不相逢
星月不相逢 2020-12-09 20:26

I\'d like to see my app in the list of player (\"continue action using...\") that pops up when I try to open an audio file (ie. from file browser or gmail attachment). Here

相关标签:
4条回答
  • 2020-12-09 20:55

    This works for me

    <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="content"/>
        <data android:scheme="file"/>
        <data android:mimeType="application/x-flac"/>
        <data android:mimeType="audio/*"/>
        <data android:mimeType="application/ogg"/>
        <data android:mimeType="application/x-ogg"/>
        <data android:mimeType="application/itunes"/>
    </intent-filter>
    
    0 讨论(0)
  • 2020-12-09 21:01

    This worked for me:

    AndroidManifest:

     <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="file" android:pathPattern=".*mp3" android:mimeType="audio/mpeg" />
     </intent-filter>
    

    MainActivity:

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {  
    
        if (Intent.ACTION_VIEW.equals(getIntent().getAction()))
        {
            File file = new File(getIntent().getData().getPath());
    
           // do what you want with the file...       
        }
    
    0 讨论(0)
  • 2020-12-09 21:03

    You always need a <category> on an <activity> <intent-filter>, as there is always at least one category on the Intent used with startActivity().

    Here is what the AOSP Music app uses:

            <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>
    
    0 讨论(0)
  • 2020-12-09 21:05

    The CommonsWare answer regarding Manifest is good and it works.

    "But when I select song and select my app from list, it opens my app successfully but not playing that selected song. what to do for that?? "

    If your app is not in background, in your activity OnCreate you can do

     Uri data = getIntent().getData()
     if (data!=null){/*use this Uri like you want*/}
    

    In case your app is in background the Intent you have to intercept will pass through the onNewIntent() method that you have to ovveride.

    Here is a complete code example:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*...*/
        interceptExternalIntentAndPlaySongFromUri(getIntent());
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        interceptExternalIntentAndPlaySongFromUri(intent);
    }
    
    private boolean interceptExternalIntentAndPlaySongFromUri(Intent intent){
        Uri data = intent.getData();
        if (data != null && intent.getAction() != null && 
    intent.getAction().equals(Intent.ACTION_VIEW)){      
            String scheme = data.getScheme();
            String filename = "";
            if ("file".equals(scheme)) {
                filename = data.getPath();
            } else {
                filename = data.toString();
            }
            player.setDataSource(filename);
            setIntent(new Intent());        //this is to remove the last intent 
    //without the above line, last request is reloaded when the user open another app 
    //and return in this
            return true;        //has intercepted an external intent. so you can load this uri.
        }
        return false;           //has not intent to intercept
    }
    
    0 讨论(0)
提交回复
热议问题