Android Audio file browsing + audio playback

Source: Internet
Author: User

After the demo runs, it will display a list of all the music files on your SD card, and you can click on the list to select a song to play.

Operating 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 runs, it will display a list of all the music files on your SD card, and you 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 run. */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};/** * You 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);/** * Determines whether the clicked Song folder or a single song */if (CurrentState = state_select_album) {/** * If it is a song file folder, then go to the list of songs */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 by = Android.Provider. Mediastore.audio.media.title;cursor = Managedquery (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Columns,where, Whereval); 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) {/** * If a song is clicked, the player of the system is called to play the song */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 code Download:

Click to download the source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.