Stefan
Stefan

Reputation: 4130

Find the Path of a Audio.Media item

I am trying to do a custom media player. Currently I am retrieving all the songs from the phone by:

audioCursor = this.managedQuery(Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, Audio.Media.TITLE+" ASC");
        startManagingCursor(audioCursor);

        String[] columnsToMap = new String[] { 
                Audio.Media.TITLE, 
                Audio.Media.ARTIST,
                Audio.Media.DISPLAY_NAME
        };
        int[] mapTo = new int[] {
                R.id.song_tf_1,
                R.id.song_tf_2,
                R.id.song_tf_path
        };

        ListAdapter mAdapter = new SimpleCursorAdapter(
                this, 
                R.layout.song_list_song,
                audioCursor, 
                columnsToMap, 
                mapTo
        );

        setListAdapter( mAdapter );

the only problem is that I don't know hot to get the PATH of the song so I can add it to a

player.reset();
player.setDataSource(songPath);
player.prepare();
player.start();

How can i get the songpath for every song and save it in a field (R.id.song_tf_path) ... Or is there any alternative I can do this better? Preferably without having to manually scan all the folders for music, etc

Upvotes: 0

Views: 572

Answers (1)

Junseok Lee
Junseok Lee

Reputation: 715

It just looks like you can replace DISPLAY_NAME with DATA and it should work out fine. Keep in mind though, it's an absolute path, and probably will be of considerable length.

Since you're using setListAdapter() I'm going to assume you're using list activity. In that case, add an onListItemClick() method in your class to perform an action when it receives a click. The method should give you an id so you know which item was clicked.

Something like this would work

protected void onListItemClick (ListView l, View v, int position, long id) {
    String[] projection = new String[] {Audio.Media._ID, Audio.Media.DATA};
    Cursor c = managedQuery(ContentUris.withAppendedId(Audio.Media.EXTERNAL_CONTENT_URI, id),
                                                    projection,
                                                    null,
                                                    null,
                                                    null);
    c.moveToFirst();
    String p = c.getString(c.getColumnIndex(Audio.Media.DATA));

    //your code to load this path into your media player
    //and play it goes here

}

The song's path will be stored in p, and you can do whatever you'd like with it.

Upvotes: 2

Related Questions