从SD卡中随机播放MP3文件

3

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

我想知道如何使用MediaPlayer从SD卡播放MP3文件。 - imran3
谢谢Imran_3,你的问题解决了我的问题。 - Kalu Khan Luhar
1个回答

2
根据NISHANT在 这里 的说法:
你需要实现流媒体播放器。这里是一个 示例。希望能帮到你。
仅供参考,我在 Google.de 上只用了30秒钟。

这并没有帮助我,我是一个Android的新手,您能更具体一些吗? - imran3
嘿,伙计。好的,你能把(String)“path”记录到LogCat中吗? - Langusten Gustel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接