Android audio file browsing + audio playback

Source: Internet
Author: User

After running the Demo, a list of all the music files on your SD card is displayed, and you can click the list to select a song for playing.

Running effect:

Click download to display:


Then, click the song to call the system player to play the video.

Source code:

Activity_audio_browser.xml:

     
      
  
 

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 run, a list of all music files on your SD card is displayed, and you can click the list to select a song for playing. **/Public class AudioBrowser extends ListActivity {private Cursor cursor; public static int Cursor = 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 a string array, indicating that when the query is run The columns returned from MediaStore. */String [] columns = {android. provider. mediaStore. audio. albums. _ ID, android. provider. mediaStore. audio. albums. ALBUM};/*** returns a list of all available ALBUM sets */cursor = managedQuery (MediaStore. audio. albums. EXTERNAL_CONTENT_URI, columns, 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 the Cursor object to Li StView 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);/*** determine whether the clicked song folder or a single song */if (currentState = STATE_SELECT_ALBUM) {/*** if it is a song folder, enter the song list of this folder */if (cursor. move ToPosition (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 orderBy = android. provider. mediaStore. audio. media. TITLE; cursor = managedQuery (MediaStore. audio. media. EXTERNAL_CONTENT_URI, columns, where, whereVal, orderBy); 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 you click a song, then the system player is called to play the song */if (cursor. moveToPosition (position) {int fileColumn = 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 source code

Related Article

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.