After the demo is executed, it will display a list of all the music files on your SD card and be able to click on the list to select a song to play.
Execution effect:
Click Download to appear:
Then click on the song to call the system player to play.
Source:
Activity_audio_browser.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <listview android:id= "@android: Id/list" android:layout_width= "Match_parent" android: layout_height= "Wrap_content" > </ListView></RelativeLayout>
Audiobrowser.java:
Package Com.multimediademo5customaudioplayer2;import Java.io.file;import Android.app.listactivity;import Android.content.intent;import Android.database.cursor;import Android.net.uri;import Android.os.Bundle;import Android.provider.mediastore;import Android.view.view;import Android.widget.listview;import android.widget.simplecursoradapter;/** * After the demo is executed, a list of all the music files on your SD card will be displayed. And can click on the list to select a song to play.* */public class Audiobrowser extends Listactivity {private Cursor cursor;public static int state_select_album = 0;public static int state_select_song = 1;private int currentstate = state_select_album; @SuppressWarnings ("deprecation") @ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_audio_browser);/** * Creates an array of strings representing the columns that will be returned from Mediastore when the query is executed.
*/string[] columns = {Android.provider.mediastore.audio.albums._id,android.provider.mediastore.audio.albums.album };/** * Returns a list of all available albums */cursor = Managedquery (mediastore.audio.albums.external_content_uri,columns, NULL, NULL, NULL) ; string[] Displayfields = new string[] {MediaStore.Audio.Albums.ALBUM};int[] displayviews = new int[] {Android. R.ID.TEXT1};/** * Can use the Setlistadapter method to bind a cursor object to a ListView object */setlistadapter (New Simplecursoradapter (this,android . R.layout.simple_list_item_1, cursor, displayfields,displayviews));} @SuppressWarnings ("deprecation") @Overrideprotected void Onlistitemclick (ListView l, View v, int position, long ID) { Super.onlistitemclick (l, V, position, id);/** * Infer whether the clicked song directory or individual song */if (CurrentState = = state_select_album) {/** * is assumed to be a song directory. The list of songs entering the directory */if (cursor.movetoposition (position)) {string[] columns = {MediaStore.Audio.Media.DATA, Mediastore.audio.media._id,mediastore.audio.media.title,mediastore.audio.media.display_name, MediaStore.Audio.Media.MIME_TYPE}; String where = android.provider.mediastore.audio.media.album+ "=?"; String whereval[] = {cursor.getstring (Cursor.getcolumnindex (MediaStore.Audio.Albums.ALBUM)}); String = Android.provider.mediastore.audio.media.title;cursor = Managedquery ( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Columns,where, Whereval, by-and-by); string[] Displayfields = new string[] {MediaStore.Audio.Media.DISPLAY_NAME};int[] displayviews = new int[] {Android. R.id.text1};setlistadapter (New Simplecursoradapter (this,android. R.layout.simple_list_item_1, Cursor,displayfields, displayviews)); currentstate = State_select_song;}} else if (CurrentState = = State_select_song) {/** * assumes that the clicked is a song, then calls the system player for song playback */if (cursor.movetoposition (position)) {int F Ilecolumn = Cursor.getcolumnindex (MediaStore.Audio.Media.DATA); int mimetypecolumn = Cursor.getcolumnindex ( MediaStore.Audio.Media.MIME_TYPE); String Audiofilepath = cursor.getstring (Filecolumn); String MimeType = cursor.getstring (mimetypecolumn); Intent Intent = new Intent (Android.content.Intent.ACTION_VIEW); File NewFile = new file (Audiofilepath); Intent.setdataandtype (Uri.fromfile (newFile), mimeType); StartActivity (Intent) ;}}}}
SOURCE Download:
Click to download source code
Android Audio file browsing + audio playback