• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to stream music from internet radio station?

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
 
I keep running into the code sample below:
Code:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
    mp.start();
MediaPlayer mp = new MediaPlayer();
    mp.setDataSource(PATH_TO_FILE);
    mp.prepare();
    mp.start();

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?
 
Upvote 0
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
 
Upvote 0
I've now guided the URL correctly:

Code:
MediaPlayer mp = MediaPlayer.create(context, [URL]http://usa2-vn.mixstream.net/8028[/URL]);

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:
android.app.Activity;
android.media.MediaPlayer;
android.os.Bundle;
android.net.Uri;
android.os.Looper;
android.media.AudioManager;
java.io.IOException;
java.net.URL;
java.net.URLConnection;
java.io.BufferedReader;
java.io.InputStreamReader;
android.content.Context;

I would simply post the whole file, but Im having to work from a public computer so no USB, etc.
 
Upvote 0
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
 
Upvote 0
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.

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
 
Upvote 0
Code:
MediaPlayer mp = MediaPlayer.create(context, Uri.parse("http://usa2-vn.mixstream.net/8028"));

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:
MediaPlayer mp = MediaPlayer.create(context, Uri.parse("[URL="http://usa2-vn.mixstream.net:8028/"][COLOR=#0000ff]http://usa2-vn.mixstream.net:8028[/COLOR][/URL]"));

But the error from eclipse is now:
Code:
MediaPlayer.create cannot be resolved into a type

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)
 
Upvote 0
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.
 
  • Like
Reactions: GIR
Upvote 0
Code:
package com.Ruthless.Streamer;
 
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.net.Uri;
import android.os.Looper;
import android.media.AudioManager;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import android.content.Context;
 
//need to read in the .m3u file to get the URL of the stream!
// m3u is a text file, but will need checking for unicode!
 
 
public class RuthlessStreamer extends Activity {
/** Called when the activity is first created. */
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//URL url = new URL("http://usa2-vn.mixstream.net/8028");
this.context = this;
//MediaPlayer mp = MediaPlayer.create(context, Uri.parse("http://usa2-vn.mixstream.net:8028"));
//mp.prepare();
MediaPlayer mp = new MediaPlayer();
mp.reset();
String link ="http://usa2-vn.mixtream.net:8028";
try {
mp.setDataSource(getApplicationContext(), Uri.parse(link));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException 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();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
//stack overflow suggests to call mp.start with an OnPreparedListener to
//avoid zero state log......
}
}

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
 
Upvote 0
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:
 MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource("http://mp1.somafm.com:8032");
        } 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();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        mp.play();
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:
if (PlayBtn != null){
            mp.start();}
        if (PlayBtn == null){
                mp.stop();
                mp.reset();   
            }
into the main chunk of the code, just after:
Code:
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
and inside the
Code:
Stop.setOnClickListener(new OnClickListener(){                 // for when the Stop button is pressed
            @Override
            // this listens for the STOP button to be pressed
            public void onClick(View v){
I inserted:
Code:
PlayBtn = "PlayButtonPressed";
inside the event handler for the play button, and:
Code:
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
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones