imran3
imran3

Reputation: 500

Play random mp3 file from sd card

I need your help. I'm developing an android app and I need to play a random song from the sd card. I tried this way :

With this method I choose randomly a song from the folder sdcard/Music (this folder contains only mp3 files).

    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];
    }

then I use this method to play the extracted song :

   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();
            }
    }

i want to know how could i play a song from the sd card of the smartphone using MediaPlayer

Upvotes: 3

Views: 2001

Answers (1)

Langusten Gustel
Langusten Gustel

Reputation: 11002

According to NISHANT here:

You Need to implement Streaming Media Player. Here is an Example. Hope it will help you.

Just for the record, it took me like 30 seconds on Google.de.

Upvotes: 2

Related Questions