音乐播放器只会列出来自" / sdcard"的音乐。

时间:2014-04-06 09:16:28

标签: java android eclipse android-sdcard audio-player android-music-player

所以我建立了我的音乐播放,它终于奏效了。感谢这里的一些特殊人士。 但起初我运行应用程序,它不会显示任何音乐。所以我看了我的代码,它说最后的字符串MEDIA_PATH =(“/ sdcard”); - 我知道,不要硬编码,但是嘿..它有效。所以我去了我的设备,并将一些音乐移动到我的SD卡的根部。打开了应用程序,它的音乐列出了“Woohoo”。

但我现在的问题是,如何让我的应用程序列出我的SD卡中的所有音乐?而不仅仅是从一个目录。 :)谢谢。

以下是我在设备上查找音乐的代码 -

package com.ascendapps.nexplay;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

import android.os.Environment;

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = ("/sdcard");
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    // Constructor
    public SongsManager(){

    }

    /*
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     */

    public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);
        if (home.listFiles(new FileExtensionFilter())!=null && home.listFiles(new FileExtensionFilter()).length > 0)
        {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }

    /*
     * The following class filters all files that have an extension of .mp3||.MP3
     */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }
}

先谢谢了,我会对此做一些研究,但我的互联网速度非常慢,而且我已经下载了Stackoverflow缓存,因此这个网站的加载速度更快。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

   cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cols, null, null, MediaStore.Audio.Media.TITLE + " ASC");
        for (int i = 0; i < cursor.getColumnCount(); i++)
            Log.d("col_info", cursor.getColumnName(i));
        count = cursor.getCount();
        cursor.moveToPosition(0);
        filePath = cursor.getString(2);
        songName = cursor.getString(0);
        artist = cursor.getString(3);
      tvSongName.setText(songName + "\n" + artist);
     ListView lv = (ListView) findViewById(R.id.listView1);
     lv.setAdapter(new MusicAdapter(getApplicationContext()));
     mediaPlayer = new MediaPlayer();
     mediaPlayer.setWakeMode(this,PowerManager.PARTIAL_WAKE_LOCK);

     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View 
        view, int i, long l) {
            cursor.moveToPosition(i);
            filePath = cursor.getString(2);
            songName = cursor.getString(0);
            artist = cursor.getString(3);
            tvSongName.setText(songName + "\n" + artist);

            try {
                if (mediaPlayer.isPlaying())
                    mediaPlayer.reset();
                mediaPlayer.setDataSource(filePath);
                mediaPlayer.prepare();
                play();
            } catch (IOException e) {
                e.printStackTrace();
            }
            rlContainer.setVisibility(View.GONE);
        }
    });

我希望能帮助你。谢谢!!