2013-05-02 96 views
0

我已經看到此MediaPlayer tutorial,而且如果有人幫我設置音頻文件,則直接從Raw文件夾播放而不是/SdCard/從原始文件夾中獲取音頻文件而不是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")); 
    } 
} 
} 

回答

3

這裏是你在找什麼

原始文件夾的訪問

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

存取SD卡

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

您的視頻在SD卡中的視頻文件夾

集媒體播放器源原始文件夾

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

謝謝兄弟(Jibran汗),但你可以告訴我,我怎樣才能把它添加到當前SongsManager.java文件? – scholar 2013-05-02 06:54:29

+0

請查看我的編輯,瞭解如何從原始文件夾將媒體播放器設置爲媒體播放器。 – 2013-05-02 06:59:00

+0

不是視頻文件,但它們是音頻。 final String MEDIA_PATH = new String(「/ sdcard /」); File home = new File(MEDIA_PATH); – scholar 2013-05-02 07:05:43

相關問題