首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >注册为音乐播放器并播放歌曲

注册为音乐播放器并播放歌曲
EN

Stack Overflow用户
提问于 2016-11-01 22:25:02
回答 5查看 1.8K关注 0票数 11

我正在创建一个音乐播放器应用程序,并且我想将该应用程序注册为音乐播放器。因此,当用户点击播放任何歌曲时,我的应用程序应该被建议作为音乐播放器之一。我跟踪了this guide

在manifest.xml中添加了必要的代码后,它只在建议中显示我的应用程序。当我选择我的应用程序时,没有播放任何内容。我认为在主活动中应该有像receiver这样的东西来接收媒体路径来播放所选的歌曲。但我不知道如何实现这一点。

我在我的manifest.xml中添加了以下代码(供参考):

代码语言:javascript
复制
<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>
EN

回答 5

Stack Overflow用户

发布于 2016-11-07 18:38:01

添加IntentFilters后,使用getIntent()从其中获取数据

代码语言:javascript
复制
@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

代码语言:javascript
复制
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
    }
}

如果您不支持从ContentProvider或从网络播放,请相应地从AndroidManifest.xml中删除"http“或"content”方案IntentFilter

票数 2
EN

Stack Overflow用户

发布于 2016-11-09 19:22:26

你有没有试过这些意图过滤器?

代码语言:javascript
复制
<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>

然后在你的活动中:

代码语言:javascript
复制
@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)
        {   ...
        }
    }

}

票数 1
EN

Stack Overflow用户

发布于 2016-11-10 01:47:06

我认为您只需在清单文件中选择歌曲时定义默认音乐播放器,并为在主线程中运行以播放歌曲的服务编写代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40362061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档