Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open music picker?

In my application, I want to make a picker that offers the user the choice to pick a music. I want to use the native android picker. I used the following code to open the native android music picker:

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

But when I execute it, I get an ActivityNotFoundException and this error message:

"Your phone has no music gallery that can be used to select a file. Please try sending a different type of files"

Am I doing something wrong there ?

like image 617
Jean-Simon Vaillant Avatar asked Aug 13 '12 16:08

Jean-Simon Vaillant


People also ask

How do I listen to audio on Android?

To play audio or video files in Android, the Android multimedia framework includes the support of MediaPlayer APIs. So, by using MediaPlayer APIs, you can play audio/video files from your Android filesystem or play files from your Application's resource file or even you can stream audio/video files just like Spotify.

How do I add music to my Android phone music app?

To add a song to My Music, use the "Find" tab at the bottom to search for music. Tap the icon with three dots (⋯) next to a song or album. Then tap Add to My Music. To access My Music, tap Library at the bottom of the screen.


2 Answers

This worked well for me:

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);

The more general intent with Intent.ACTION_GET_CONTENT can present the user with a number of options for activities to pick an audio file (Astro file manager, etc.). However, the user could select any file, not necessarily an audio file. I wanted one that simply allowed the user to select an audio file from their media. This did the trick.

like image 141
Craig B Avatar answered Oct 04 '22 19:10

Craig B


If you take a look at the AndroidManifest.xml file for the latest core Music app, it may shed some light on the options that you have. For example:

<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>

So based on this, you might first try

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

and see if it fits your needs. You may also look at adding the category flags noted in the above example to help narrow down the results (e.g. OPENABLE should filter to only content that can be opened as a stream.

like image 31
devunwired Avatar answered Oct 04 '22 21:10

devunwired