Saturday, February 23, 2019

Android play music in sd card


We want to play music in sd card or phone storage.
Declare array, variables.
MediaPlayer mPlayer;
ArrayList<String> mMusicList=new ArrayList<String>();
ArrayList<String> mAudioPath=new ArrayList<String>();
Copy these function in to above last close bracket.
private void playSong(String path) throws IllegalArgumentException,
         IllegalStateException, IOException {
         mPlayer.reset();
         mPlayer.setDataSource(path);      
         //mPlayer.setLooping(true);
         mPlayer.prepare();
         mPlayer.start();
   }   
private ArrayList<String> getAudioList() {          
final Cursor mCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null,"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
ArrayList<String> songs=new ArrayList<String>();
          int i = 0;
     if (mCursor.moveToFirst()) {
             do {               mAudioPath.add(mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
songs.add(mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));                      
           i++;
            } while (mCursor.moveToNext());
     }  
mCursor.close();
return songs;
}        
Create a xml file in folder layout, it has a listview.
<ListView
     android:id="@+id/li"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_above="@+id/t"
     android:layout_gravity="center"
     android:layout_marginTop="3dp" >
 </ListView>
Add permission to file manifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In play class, copy to below setcontentView();
mPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.li);
mMusicList = getAudioList();
for(int i=0;i<mMusicList.size();i++){
     for(int j=1;j<mMusicList.size();j++){
     if(mMusicList.get(j).equals(mMusicList.get(i))) {
          mMusicList.remove(j);
          mAudioPath.remove(j);
          }
     }
}
The for loop aim to remove duplicate song may have in sd card or phone.
Copy command for listView to continue.
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
     try {
     playSong(mAudioPath.get(arg2));
     } catch (IllegalArgumentException e) {
     e.printStackTrace();
     } catch (IllegalStateException e) {
     e.printStackTrace();
     } catch (IOException e) {
     e.printStackTrace();
     }
     }
});
That ‘s all, run and press rows, choose another row to next songs.
The song name contain singer name, you can sub or split String if you like.
Add these lines to destroy when exit class.
@Override
     public void onDestroy() {
          super.onDestroy();
          if (mPlayer != null) {
               mPlayer.release();
          }
           

}

No comments:

Post a Comment