从SD卡播放随机mp3文件

时间:2012-09-19 13:18:04

标签: android media-player

我需要你的帮助。 我正在开发一个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卡播放歌曲

1 个答案:

答案 0 :(得分:2)

根据NISHANT here

  

您需要实现流媒体播放器。这是一个Example。希望它会对你有所帮助。

仅仅为了记录,我在Google.de上花了30秒钟。

相关问题