2015-05-20 30 views
0

我想從特定文件夾獲取所有歌曲。目前,我正在獲取所有歌曲,然後根據路徑進行循環播放並獲取特定路徑中的歌曲。任何更好的方法可以做到這一點?從特定路徑獲取所有歌曲

for (int i = 0; i < totalSongList.size(); i++) { 
     String path = totalSongList.get(i).getPathId(); 
     int index = path.lastIndexOf("/"); 
     String folderPath1 = path.substring(0, index); 

     if (folderPath.equals(folderPath1)) 
      songList.add(totalSongList.get(i)); 

    } 

我不能使用下面的代碼,因爲它也會獲取子文件夾歌曲。

musicResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.Media.DATA + " like ? ", 
     new String[] {"%SPECIFIC_FOLDER_NAME%"}, null); 
+0

您可以編寫java代碼來搜索特定文件夾中的所有歌曲,而不是使用mediastore。 –

+0

你能舉個相同的例子嗎? – RisingUp

回答

0

這裏是Java代碼,而無需使用mediaStore用於從其可能或maynot包含任何子目錄的特定文件夾中檢索所有歌曲。

public String File path = new File("your folder path here"); 

public void Search(File dir) 
{ 
if(dir.isFile()) 
{ 
//get necessary songName over here 
String songName = dir.getName(); 

//if you need path 
String songPath = dir.getAbsolutePath(); 

//if your folder consists of other files other than audio you have to filer it 

if(songName.endsWith(".mp3") || songName.endsWith(".MP3") 
{ 
//this is the required mp3 files 
//do what you want to do with it 
} 

}else if(dir.isDirectory()) 
{ 
File[] list = dir.listFiles(); 

for(int i=0;i<list.length();i++) 
{ 
search(list[i]); 
} 

} 
else 
{ 
//handle exceptions here 
}  


} 
相關問題