我需要你的帮助。我正在开发一个Android应用程序,需要从SD卡播放随机歌曲。我尝试了以下方法:
使用这种方法,我从文件夹 sdcard/Music
(该文件夹仅包含mp3文件)中随机选择一首歌曲。
public File chooseSong()
{
Random r=new Random();
File path=new File("/sdcard/Music");
File[] songsList=path.listFiles();
int index=(r.nextInt(songsList.length));
Toast.makeText(Main.this, "Song extract "+songsList[index],Toast.LENGTH_SHORT).show();
return songsList[index];
}
然后我使用这种方法来播放提取出来的歌曲:
public void play()
{
Toast.makeText(Main.this, "in method play() ", Toast.LENGTH_SHORT).show();
try
{
File f=chooseSong();
String path=f.getPath();
mpSong = new MediaPlayer();
mpSong.setDataSource(path);
mpSong.prepare(); //i think the problem is here, i receive "failed to prepare status 0x1"
mpSong.start();
Toast.makeText(Main.this, "Playing", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(Main.this, "error", Toast.LENGTH_SHORT).show();
}
}
我想知道如何使用MediaPlayer从智能手机的SD卡播放歌曲。