How to stream music from internet radio station?


Last Updated:

  1. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    Hello fellow coders!

    I've been through the tutorials, and have started on the learning curve, however when I try any combination of examples available that promise to stream audio I have run into problems, the main one being syntax.

    Could anyone please be so kind as to point me in the direction of suitable sources?

    Im using Eclipse on ubuntu, and the end target is 2.1 (ZTE racer).

    Many thanks for taking the time to read this, and for any help.

    GIR
     

    Advertisement
  2. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    GIR likes this.
  3. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    I keep running into the code sample below:
    Code (Text):
    1.  
    2. MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
    3.     mp.start();
    4. MediaPlayer mp = new MediaPlayer();
    5.     mp.setDataSource(PATH_TO_FILE);
    6.     mp.prepare();
    7.     mp.start();
    8.  
    But when I start to enter this into eclipse I fall at the first hurdle.
    When entering the line :

    'MediaPlayer mp = MediaPlayer.create(context, http://usa2-vn.mixtream.net/8028.m3u);' ,

    eclipse throws up an error: 'context cannot be resolved into a variable'.

    What am i doing wrong?
     
  4. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    perhaps it happens because http://usa2-vn.mixtream.net/8028.m3u is not a stream.
    Try opening the file with a texteditor. You will find 1 or more stream urls, which you have to put instead of the .m3u.
     
  5. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    The pc im using to access this forum is locked down (public) and wont let me open the file.

    Could someone please be kind enough to post the contents as suggested by id0001?

    Also no matter what stream URL I use in my code I still get the error message about the context.

    Any and all help and suggestions greatly appreciated.

    Many thanks,
    GIR
     
  6. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    are you sure the server exists? because I can't ping 'usa2-vn.mixtream.net'
     
  7. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    *double post*
     
  8. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    It is live, though sometimes a break in transmission does occur.
    If someone could please post the contents of the .m3u playlist I'd be very gratefull.

    Many thanks,
    GIR
     
  9. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
  10. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    visit ww.ruthlesssoundz.com, the link plays fine on my androidradio player, and was copied from the link at the top right of the page.

    The correct link is http://usa2-vn.mixstream.net/8028.m3u

    sorry, that was a typo - my fault.
     
  11. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    GIR likes this.
  12. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    id0001,
    Many thanks for sticking by and being helpful.
    I used wikipedia (M3U - Wikipedia, the free encyclopedia) and discovered that there should be some text and/or comments in the .m3u file.
    I had hoped to be able to read the file with my app and then start streaming the appropiate mp3 stream...

    This leaves the following questions before I can mark this thread as solved, etc:
    How can I open the .m3u file to read the name/url of the stream, and how is the error corrected from eclipse about the 'context' error?

    Many thanks for any help,
    GIR
     
  13. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    open the file as text.
    Then go through the file and extract all the url's

    it's easy.

    p.s. can you post the whole class?
     
  14. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    I cant open the file on this bloody windows pc - its a public library pc so I cant use my usual 'workarounds' to get the file open. (Im awaiting a new powersupply for my Ubuntu pc).

    I'll try to attract some attention to this.
     
  15. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    I've now guided the URL correctly:

    Code (Text):
    1.  
    2. MediaPlayer mp = MediaPlayer.create(context, [URL]http://usa2-vn.mixstream.net/8028[/URL]);
    3.  
    However Eclipse is throwing up:
    context cannot be resolved to a variable

    Eclipse is set with a min sdk of 7.
    Are there any permissions that need to be set to allow the mediaplayer to run?
    Why is Eclipse complaining about the error?

    I currently have the following import:
    Code (Text):
    1.  
    2. android.app.Activity;
    3. android.media.MediaPlayer;
    4. android.os.Bundle;
    5. android.net.Uri;
    6. android.os.Looper;
    7. android.media.AudioManager;
    8. java.io.IOException;
    9. java.net.URL;
    10. java.net.URLConnection;
    11. java.io.BufferedReader;
    12. java.io.InputStreamReader;
    13. android.content.Context;
    14.  
    I would simply post the whole file, but Im having to work from a public computer so no USB, etc.
     
  16. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    are you setting the context variable to you current activity?

    Like:
    Code (Text):
    1. Context context;
    2.  
    3. protected void onCreate(Bundle savedBundle) {
    4.    super.onCreate(savedBundlde);
    5.  
    6.    this.context = this;
    7. }
     
    GIR likes this.
  17. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    I am now :)

    However Eclipse is still complaining and throwing an error:

    The method create(Context, Uri) in the type MediaPlayer is not applicable for the arguments (Context, String)

    While trawling through Google results, (and searching this forum) I come across conflicting information (for a learner) about different structures of syntax for the different target devices, which is not always explained on the sites.

    Many thanks for any and all help,
    GIR
     
  18. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    Ah I know what your problem is!

    The types 'Uri' and 'String' are not the same.
    Uri | Android Developers

    You can parse your string to a Uri with the 'Uri.parse(String uriString)' method.
     
  19. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    I followed the link and read, however I cannot find any code samples or tutorials that cover this (yet... im still looking).

    I've spent hours now trying many different iterations of code revolving around the info on the android devlopers site, so far eclipse is still complaining.

    Could anyone be kind enough to point me in the direction of any examples, etc or even show a working example?

    Dont get me wrong, Im not asking for anyone to do my learning for me just that I need a little more guidance.

    Many thanks,
    GIR
     
  20. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    Code (Text):
    1. MediaPlayer mp = MediaPlayer.create(context, Uri.parse("http://usa2-vn.mixstream.net/8028"));
     
    GIR likes this.
  21. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    Id0001, many thanks for the code snippet!
    This is starting to feel like 3 steps forward and 2 back.

    I'm wondering what I've got set wrong, because I still get errors from eclipse.

    I altered my code to:
    Code (Text):
    1.  
    2. MediaPlayer mp = MediaPlayer.create(context, Uri.parse("[URL="http://usa2-vn.mixstream.net:8028/"][COLOR=#0000ff]http://usa2-vn.mixstream.net:8028[/COLOR][/URL]"));
    3.  
    But the error from eclipse is now:
    Code (Text):
    1.  
    2. MediaPlayer.create cannot be resolved into a type
    3.  
    How can I solve this?

    Also, eclipse is complaining about my use of mp.prepare();
    "Unhandled execption type IOException"

    This is frustrating because I find tutorials which are aimed at 1.5, but few for either 2.0,2.1, or even 2.2 (I would use 2.2 IF I could be sure of 'backwards' compatability)
     
  22. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    Most of the 1.5 tutorials are also applicable to later versions.
    If you are having an eclipse error you probably made a typo in the syntax or you don't import the right package.
    When you click on the error icon on the left of the line where the error is on you get suggestions for a possible solution. You should first check these out before you go on the forum and ask about these errors.

    If a type cannot be resolved it's probably a package you didn't import.
    When an exception is unhandled it means that the method has a 'throws' clause. You need to put a try/catch block around this method call. Also this can be solved by clicking on the error icon on the left of the line.

    Please search for the solution yourself first before asking. Most times there is a really simple solution which you can find by reading the error and the possible solutions which eclipse gives.
     
    GIR likes this.
  23. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    Code (Text):
    1.  
    2. package com.Ruthless.Streamer;
    3.  
    4. import android.app.Activity;
    5. import android.media.MediaPlayer;
    6. import android.os.Bundle;
    7. import android.net.Uri;
    8. import android.os.Looper;
    9. import android.media.AudioManager;
    10. import java.io.IOException;
    11. import java.net.URL;
    12. import java.net.URLConnection;
    13. import java.io.BufferedReader;
    14. import java.io.InputStreamReader;
    15. import android.content.Context;
    16.  
    17. //need to read in the .m3u file to get the URL of the stream!
    18. // m3u is a text file, but will need checking for unicode!
    19.  
    20.  
    21. public class RuthlessStreamer extends Activity {
    22. /** Called when the activity is first created. */
    23. Context context;
    24. @Override
    25. public void onCreate(Bundle savedInstanceState) {
    26. super.onCreate(savedInstanceState);
    27. setContentView(R.layout.main);
    28. //URL url = new URL("http://usa2-vn.mixstream.net/8028");
    29. this.context = this;
    30. //MediaPlayer mp = MediaPlayer.create(context, Uri.parse("http://usa2-vn.mixstream.net:8028"));
    31. //mp.prepare();
    32. MediaPlayer mp = new MediaPlayer();
    33. mp.reset();
    34. String link ="http://usa2-vn.mixtream.net:8028";
    35. try {
    36. mp.setDataSource(getApplicationContext(), Uri.parse(link));
    37. } catch (IllegalArgumentException e) {
    38. // TODO Auto-generated catch block
    39. e.printStackTrace();
    40. } catch (SecurityException e) {
    41. // TODO Auto-generated catch block
    42. e.printStackTrace();
    43. } catch (IllegalStateException e) {
    44. // TODO Auto-generated catch block
    45. e.printStackTrace();
    46. } catch (IOException e) {
    47. // TODO Auto-generated catch block
    48. e.printStackTrace();
    49. }
    50.  
    51. try {
    52. mp.prepare();
    53. } catch (IllegalStateException e) {
    54. // TODO Auto-generated catch block
    55. e.printStackTrace();
    56. } catch (IOException e) {
    57. // TODO Auto-generated catch block
    58. e.printStackTrace();
    59. }
    60. mp.start();
    61. //stack overflow suggests to call mp.start with an OnPreparedListener to
    62. //avoid zero state log......
    63. }
    64. }
    65.  
    I found the 'auto-insert' function which does help in catching the errors, but it merely seems to allow the program to run and does not actually solve inform me of what is going wrong.

    I ran the above code in the emulator, and it seems to work ok, but when I exported to an .apk and installed to my phone the phone shows only a breif activity of traffic on the 3G meter, and then stops.

    I inserted some toasts to show me where the program is failing to work correctly and I get an IOException error, but no further details so I went searching!

    Could this be because the stream is not named?
    I noted that in the pockeyjorney site tutorial (for 1.5) has a section where buffering the stream is done before its played back via MediaPlayer, and I would appreciate any form of confirmation that a streams url needs to end with .mp3 ....

    Many thanks in advance,
    GIR
     
  24. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    I'm revisting this.

    It seems that the stream was turned off! Shame I wasn't updated by the folk over at the site didn't let me know!!

    Anyhow,

    When I simply run :
    Code (Text):
    1.  
    2.  MediaPlayer mp = new MediaPlayer();
    3.         try {
    4.             mp.setDataSource("http://mp1.somafm.com:8032");
    5.         } catch (IllegalArgumentException e) {
    6.             // TODO Auto-generated catch block
    7.             e.printStackTrace();
    8.         } catch (IllegalStateException e) {
    9.             // TODO Auto-generated catch block
    10.             e.printStackTrace();
    11.         } catch (IOException e) {
    12.             // TODO Auto-generated catch block
    13.             e.printStackTrace();
    14.         }
    15.         try {
    16.             mp.prepare();
    17.         } catch (IllegalStateException e) {
    18.             // TODO Auto-generated catch block
    19.             e.printStackTrace();
    20.         } catch (IOException e) {
    21.             // TODO Auto-generated catch block
    22.             e.printStackTrace();
    23.         mp.play();
    24.  
    It works!

    I've Added a PLAY and STOP button to the UI, and am having difficulty in getting the buttons to make the player start and stop.

    This is most likely a noob issue.

    First I added :
    Code (Text):
    1.  
    2. if (PlayBtn != null){
    3.             mp.start();}
    4.         if (PlayBtn == null){
    5.                 mp.stop();
    6.                 mp.reset();  
    7.             }
    8.  
    into the main chunk of the code, just after:
    Code (Text):
    1.  
    2. @Override
    3.     public void onCreate(Bundle savedInstanceState) {
    4.         super.onCreate(savedInstanceState);
    5.         setContentView(R.layout.main);
    6.  
    and inside the
    Code (Text):
    1.  
    2. Stop.setOnClickListener(new OnClickListener(){                 // for when the Stop button is pressed
    3.             @Override
    4.             // this listens for the STOP button to be pressed
    5.             public void onClick(View v){
    6.  
    I inserted:
    Code (Text):
    1.  
    2. PlayBtn = "PlayButtonPressed";
    inside the event handler for the play button, and:
    Code (Text):
    1.  
    2. PlayBtn = null;
    Inside the stop button, however the media player never even attempts to stream (I tried in both the emulator and my actual phone istelf)

    What am I doing wrong?

    Thanks for any help,
    GIR
     
  25. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    bump
     

Share This Page

Loading...