0

I have created a simple MP3 player using listview. It currently only shows the MP3 files stored on a SD card. How can I change my code so both locally stored music and the SD card MP3 files will show in the list view.

package example.mp3player;


import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;


public class MainActivity extends ListActivity {
Button stop;
private static final String SD_PATH = new String("/sdcard/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        stop = (Button) findViewById(R.id.stopbtn);
        updatePlayList();
    }



    private void setButtonOnClickListeners(){
        stop.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) { 
                mp.stop();
            }
        });}

    @Override
    protected void onListItemClick(ListView list, View view, int position, long id){
    try{
    mp.reset();
    mp.setDataSource(SD_PATH + songs.get(position));
    mp.prepare();
    mp.start();
    }catch(IOException e){
    Log.v(getString(R.string.app_name), e.getMessage());
    }}


        private void updatePlayList() {
            File home = new File(SD_PATH);
            if (home.listFiles( new Mp3Filter()).length > 0){
            for (File file : home.listFiles( new Mp3Filter())){
            songs.add(file.getName());
            }

            ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_item,songs);
            setListAdapter(songList);
            }

        }

        class Mp3Filter implements FilenameFilter{
            public boolean accept(File dir, String name){
            return (name.endsWith(".mp3"));
            }
        }
    }
Phil3992
  • 1,059
  • 6
  • 21
  • 45
  • What is difference between locally stored music and music stored on SD card? are not those locally stored music are in SD card also? – M.Baraka Sep 22 '15 at 13:02
  • Good point yes they are both local. My code detects the mp3 on SD cards but not on the device. I would like to be able to list all the songs like the default music app on smart phones. So I need to show music stored on SD and stored on the device storage. – Phil3992 Sep 22 '15 at 19:15

0 Answers0