Streaming audio .m3u

I want to play streaming radio (.m3u format), but I don’t know how to do it.

In this example, as I try to reproduce:

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 me please.

+6
source share
2 answers

You must first download the M3U file. This is just a text file, read it line by line. Each line will have a link that 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; } } 
+13
source

I had the same issue with streaming radio. But in my case, I just removed .m3u from the url and it will work!

Try to do this:

 mp.setDataSource("url"); 

instead

 mp.setDataSource("url.m3u"); 
+6
source

Source: https://habr.com/ru/post/892727/


All Articles