Amazon Gift Cards

Wednesday, January 7, 2009

Searching Audio Files in Android

Searching for Audio Files in Android is so much similar in Display Contact Names in Android (I suggest you to look back at the explanation for a more detailed explanation). The audio formats that Android allows are MP3, AAC, AMR.
public class Main extends ListActivity {
  private Cursor audioCursor;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    audioCursor = this.managedQuery(Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
    startManagingCursor(audioCursor);

    String[] columnsToMap = new String[] {Audio.Media.TITLE , Audio.Media._ID};
    int[] mapTo = new int[] {android.R.id.text1,android.R.id.text2};

    ListAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item , audioCursor, columnsToMap, mapTo);
    this.setListAdapter(mAdapter);
  }
}


Quick Explanation
The thing that you need here is Audio.Media.EXTERNAL_CONTENT_URI, as stated on the the contact display article, managedQuery acts like a sql call and the table this time is Audio.Media.EXTERNAL_CONTENT_URI.
Another thing you might have notice is String[] columnsToMap = new String[] {Audio.Media.TITLE , Audio.Media._ID};. These and a lot more are the columns that can be used.

Source Code
Main.java file for Searching Audio in Android

References
android.provider.MediaStore.Audio.Media
Display Contact Names in Android



Update History
   Jan 17, 2012 - Visual Update

3 comments:

techy_dev said...

Could you help me i want to know how i can search for album art then display it next to the artist name and song name like in the android music app could you show me how to do that? Please

eliran said...

Hi.
Thanks for the example!
Can you please show how to start playing the chosen song with MediaPlayer object? In other words, how can i know full path to the song in order to start playing it?

Thanks alot

eliran

test said...

Here is a description of another way to search for audio files on android