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 卡播放歌曲

4

1 回答 1

2

根据 NISHANT这里

您需要实现流媒体播放器。这是一个例子。希望它会帮助你。

只是为了记录,我在Google.de上花了大约 30 秒。

于 2012-09-19T14:47:48.090 回答