搜索音乐播放器的电话和SD卡的整个存储空间

时间:2015-07-05 13:00:54

标签: java android eclipse

当我开始制作音乐播放器时,只需在SD Card中搜索即可,并显示音乐,播放器也能正常工作。几周前,我买了一部新手机并决定将应用程序安装到它上面,但是当我加载应用程序时,它找不到任何音乐,所以我需要一种方法来更改当前代码,以便它可以搜索每一个任何以“.mp3”结尾的文件的电话文件夹和micro SD Card

主要Activity.java

class Mp3Filter implements FilenameFilter {
public boolean accept(File dir, String name) {
    return (name.endsWith(".mp3"));
    }
}

public class MainActivity extends ListActivity implements OnCompletionListener {
private static final String SD_PATH = new String(Environment.getExternalStorageDirectory().getPath() + "/");
private static final String PHONE_STORAGE = new String(Environment.getRootDirectory().getPath() + "/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
private View play;
private View pause;
private View stop;
private View next;
private View prev;
private View replay;
private View shuffle;
private boolean isRepeat = false;
private boolean isShuffle = false;
public static int SONG_NUMBER = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    updatePlaylist();
    play = (ImageButton) findViewById(R.id.imageButton1);
    pause = (ImageButton) findViewById(R.id.imageButton2);
    stop = (ImageButton) findViewById(R.id.imageButton3);
    next = (ImageButton) findViewById(R.id.imageButton4);
    prev = (ImageButton) findViewById(R.id.imageButton5);
    replay = (ImageButton) findViewById(R.id.imageButton6);
    shuffle = (ImageButton) findViewById(R.id.imageButton7);
    mp.setOnCompletionListener(this);
    play.setEnabled(false);
    pause.setEnabled(false);
    stop.setEnabled(false);
    next.setEnabled(false);
    prev.setEnabled(false);
    replay.setEnabled(false);
    shuffle.setEnabled(false);

}

@Override
protected void onListItemClick(ListView list, View view, int position,
        long id) {
    try {
        SONG_NUMBER = position;
        mp.reset();
        mp.setDataSource(SD_PATH + PHONE_STORAGE + songs.get(position));
        mp.prepare();
        mp.start();
        play.setEnabled(false);
        pause.setEnabled(true);
        stop.setEnabled(true);
        next.setEnabled(true);
        prev.setEnabled(true);
        replay.setEnabled(true);
        shuffle.setEnabled(true);
    } 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);
    }
    File secondDirectory = new File(PHONE_STORAGE);
    if (secondDirectory.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);
    }
}

public void play(View view) {
    Toast.makeText(getApplicationContext(), "Playing song",
            Toast.LENGTH_SHORT).show();
    mp.start();
    play.setEnabled(false);
    pause.setEnabled(true);
    stop.setEnabled(true);
    next.setEnabled(true);
    prev.setEnabled(true);
    replay.setEnabled(true);
    shuffle.setEnabled(true);
}

public void pause(View view) {
    Toast.makeText(getApplicationContext(), "Pausing song",
            Toast.LENGTH_SHORT).show();
    mp.pause();
    play.setEnabled(true);
    pause.setEnabled(false);
    stop.setEnabled(true);
    next.setEnabled(true);
    prev.setEnabled(true);
    replay.setEnabled(false);
    shuffle.setEnabled(false);
}

public void stop(View view) {
    Toast.makeText(getApplicationContext(), "Stopping song",
            Toast.LENGTH_SHORT).show();
    mp.stop();
    play.setEnabled(false);
    pause.setEnabled(false);
    stop.setEnabled(false);
    next.setEnabled(false);
    prev.setEnabled(false);
    replay.setEnabled(false);
    shuffle.setEnabled(false);
    isRepeat = false;
    isShuffle = false;
}

public void next(View view) {
    isRepeat = false;
    try {
        if (isShuffle) {
            mp.reset();
            Random rand = new Random();
            SONG_NUMBER = rand.nextInt((songs.size() - 1) - 0 + 1) + 0;
            mp.setDataSource(SD_PATH + PHONE_STORAGE + songs.get(SONG_NUMBER));
            mp.prepare();
            mp.start();
        }
        else {
        if (SONG_NUMBER < songs.size() - 1) {
            Toast.makeText(getApplicationContext(), "Next song",
                    Toast.LENGTH_SHORT).show();
            mp.reset();
            mp.setDataSource(SD_PATH + PHONE_STORAGE + songs.get(SONG_NUMBER));

        } else {
            Toast.makeText(getApplicationContext(), "Please try again",
                    Toast.LENGTH_SHORT).show();
            SONG_NUMBER = -1;
        }
        mp.start();
        }
    } catch (IOException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    }
}

public void prev(View view) {
    isRepeat = false;
    isShuffle = false;
    try {
        if (SONG_NUMBER == 0) {
            Toast.makeText(getApplicationContext(),
                    "Cannot play previous song", Toast.LENGTH_SHORT).show();
            prev.setEnabled(false);
        } else {
            Toast.makeText(getApplicationContext(), "Previous song",
                    Toast.LENGTH_SHORT).show();
            mp.reset();
            mp.setDataSource(SD_PATH + PHONE_STORAGE + songs.get(SONG_NUMBER));
            SONG_NUMBER -= 2;
        }
        mp.start();
    } catch (IOException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    } catch (NullPointerException e) {
        Log.v(getString(R.string.prevButton), e.getMessage());
    }
}

public void replay(View view) {

    if (isRepeat) {
        Toast.makeText(getApplicationContext(), "Replaying All Songs",
                Toast.LENGTH_SHORT).show();
        isRepeat = false;
    } else {
        Toast.makeText(getApplicationContext(), "Replaying Current Song",
                Toast.LENGTH_SHORT).show();
        isRepeat = true;
        isShuffle = false;
    }

}

public void shuffle(View view) {
    if (isShuffle) {
        Toast.makeText(getApplicationContext(), "Playing Songs Normally",
                Toast.LENGTH_SHORT).show();
        isShuffle = false;
    } else {
        Toast.makeText(getApplicationContext(), "Shuffling Songs",
                Toast.LENGTH_SHORT).show();
        isShuffle = true;
        isRepeat = false;
    }
}

@Override
public void onCompletion(MediaPlayer mp1) {
    try {
        if (isRepeat) {
            mp.start();
        } else if (isShuffle) {
            mp.reset();
            Random rand = new Random();
            SONG_NUMBER = rand.nextInt((songs.size() - 1) - 0 + 1) + 0;
            mp.setDataSource(SD_PATH + songs.get(SONG_NUMBER));
            mp.prepare();
            mp.start();
        } else {
            mp.reset();
            SONG_NUMBER += 1;
            mp.setDataSource(SD_PATH + songs.get(SONG_NUMBER));
            mp.prepare();
            mp.start();
        }
        play.setEnabled(false);
        pause.setEnabled(true);
        stop.setEnabled(true);
        next.setEnabled(true);
        prev.setEnabled(true);
        replay.setEnabled(true);
        shuffle.setEnabled(true);
    } catch (IOException e) {
        Log.v(getString(R.string.app_name), e.getMessage());
    }

}

public void onDestroy() {
    super.onDestroy();
    mp.release();
}
}

1 个答案:

答案 0 :(得分:1)

您无需搜索存储中的单个文件。 MediaStore为您完成。每个音频文件都将在此处列出

<强>更新

String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

            String[] projection = {
                    MediaStore.Audio.Media.DATA,
                    };

            Cursor cursor = getActivity().managedQuery(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    projection,
                    selection,
                    null,
                    null);

            while(cursor.moveToNext()){
            //do something with the path
            File f=new File(cursor.getString(0));
            System.out.println(cursor.getString(0));
            }