如何打开音乐选择器?

问题描述 投票:13回答:3

在我的应用程序中,我想制作一个选择器,为用户提供选择音乐的选择。我想使用原生的android选择器。我使用以下代码打开本机安卓音乐选择器:

final Intent intent2 = new Intent(Intent.ACTION_PICK);
intent2.setType("audio/*");
startActivityForResult(intent2, 1);

但是当我执行它时,我得到一个ActivityNotFoundException和这个错误消息:

“您的手机没有可用于选择文件的音乐库。请尝试发送不同类型的文件”

我在那里做错了吗?

android gallery
3个回答
17
投票

这对我很有用:

public static final int REQ_PICK_AUDIO = 10001;
//------   
Intent audio_picker_intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
activity.startActivityForResult(audio_picker_intent, REQ_PICK_AUDIO);

使用Intent.ACTION_GET_CONTENT的更一般意图可以为用户提供许多选项来选择音频文件(Astro文件管理器等)。但是,用户可以选择任何文件,不一定是音频文件。我想要一个只允许用户从他们的媒体中选择一个音频文件。这样做了。


7
投票

如果您查看AndroidManifest.xml文件以获取最新的核心音乐应用程序,它可能会对您拥有的选项有所了解。例如:

<activity android:name="com.android.music.MusicPicker"
        android:label="@string/music_picker_title" android:exported="true" >
    <!-- First way to invoke us: someone asks to get content of
         any of the audio types we support. -->
    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.OPENABLE" />
        <data android:mimeType="audio/*"/>
        <data android:mimeType="application/ogg"/>
        <data android:mimeType="application/x-ogg"/>
    </intent-filter>
    <!-- Second way to invoke us: someone asks to pick an item from
         some media Uri. -->
    <intent-filter>
        <action android:name="android.intent.action.PICK" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.OPENABLE" />
        <data android:mimeType="vnd.android.cursor.dir/audio"/>
    </intent-filter>
</activity>

所以基于此,你可能会先尝试

final Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);
intent2.setType("audio/*");
startActivityForResult(intent2, 1);

并看看它是否符合您的需求。您还可以查看添加上面示例中提到的类别标记以帮助缩小结果范围(例如,OPENABLE应该仅过滤为可以作为流打开的内容。


0
投票

这可能有用

// some Intent that points to whatever you like to play
Intent play = new Intent(Intent.ACTION_VIEW);
play.setData(Uri.fromFile(new File("/path/to/file")));
// create chooser for that intent
try {
    Intent i = Intent.createChooser(play, "Play Music");
    c.startActivity(i);
} catch(ActivityNotFoundException ex) {
    // if no app handles it, do nothing
}
© www.soinside.com 2019 - 2024. All rights reserved.