从原始文件夹而不是SdCard获取音频文件

时间:2013-05-02 06:42:51

标签: android media-player

我看过这个媒体播放器tutorial,我想知道是否有人帮助我直接从Raw文件夹而不是/SdCard/设置音频文件?

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

public class SongsManager {
// SDCard Path

@SuppressLint("SdCardPath")

final String MEDIA_PATH = new String("/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()).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;
}

/**
 * Class to filter files which are having .mp3 extension
 * */
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}
}

1 个答案:

答案 0 :(得分:3)

以下是您要找的内容

原始文件夹访问

 Uri uri=Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.test);

SD卡访问

 final Uri uri=Uri.parse(Environment.getExternalStorageDirectory()+"/Video/tmn.wmv");

您的视频位于SDCARD的视频文件夹中

将媒体播放器源设置为原始文件夹

MediaPlayer mediaPlayer = new MediaPlayer();
Uri uri=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.test);
mediaPlayer.setDataSource(this,uri);