简体   繁体   中英

streaming .m3u audio

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.

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

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM