7

I want play streaming radio( .m3u format ), but i do not know how do it.

This example how i try playing:

final MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource("url.m3u");
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mp.prepare();
            mp.start();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

this code does not work. help please.

alezhka
  • 738
  • 2
  • 12
  • 29
  • may be this one help you :: http://stackoverflow.com/questions/6283568/online-radio-streaming-app-for-android http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/ – Nikunj Patel Jul 14 '11 at 05:19
  • check this link: https://github.com/abdullahfarwees/Android-Online-Radio-app – Abdullah Farweez Sep 27 '18 at 14:14

2 Answers2

13

You have to download the M3U file first. It's just a text file, read it line by line. Each line will have a link which you can read in your media player.

Use something like this,

public ArrayList<String> readURLs(String url) {             
        if(url == null) return null;
        ArrayList<String> allURls = new ArrayList<String>();
        try {

            URL urls = new URL(url);
            BufferedReader in = new BufferedReader(new InputStreamReader(urls
                    .openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                allURls.add(str);
            }
            in.close();
            return allURls ;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } 
    }
Hades
  • 3,916
  • 3
  • 34
  • 74
10

I had the same issue with streaming radio. But in my case I've just removed .m3u from url and it worked!

Try to do this:

mp.setDataSource("url");

instead

mp.setDataSource("url.m3u");
valerybodak
  • 4,195
  • 2
  • 42
  • 53