39

I'm building an Android app that will stream several radio stations from a Latin Country, there is like 10 stations that I know can be played in android, I got the URL's from them and actually made them work using this tutorial (link removed, because it is dead) but the problem I have is that it plays for several seconds and then stops it keeps loading but does not restart the streaming, my questions:

  • If someone has worked with this tutorial can explain me how to make it stream constantly with out stopping.
  • Is there an easier way to stream radio audio? this tutorial seems kind of old, is there a newer tutorial or a newer code sample to study or use?
  • Can someone send me the right way?
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
zvzej
  • 6,276
  • 7
  • 33
  • 41

3 Answers3

65

So I found this sample and it works for me, here it is if you have the same issue:

in myMain.java

import android.app.Activity;
import android.os.Bundle;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class myMain extends Activity implements OnClickListener {

    private ProgressBar playSeekBar;

    private Button buttonPlay;

    private Button buttonStopPlay;

    private MediaPlayer player;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initializeUIElements();

        initializeMediaPlayer();
    }

    private void initializeUIElements() {

        playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
        playSeekBar.setMax(100);
        playSeekBar.setVisibility(View.INVISIBLE);

        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(this);

        buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
        buttonStopPlay.setEnabled(false);
        buttonStopPlay.setOnClickListener(this);

    }

    public void onClick(View v) {
        if (v == buttonPlay) {
            startPlaying();
        } else if (v == buttonStopPlay) {
            stopPlaying();
        }
    }

    private void startPlaying() {
        buttonStopPlay.setEnabled(true);
        buttonPlay.setEnabled(false);

        playSeekBar.setVisibility(View.VISIBLE);

        player.prepareAsync();

        player.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                player.start();
            }
        });

    }

    private void stopPlaying() {
        if (player.isPlaying()) {
            player.stop();
            player.release();
            initializeMediaPlayer();
        }

        buttonPlay.setEnabled(true);
        buttonStopPlay.setEnabled(false);
        playSeekBar.setVisibility(View.INVISIBLE);
    }

    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        try {
            player.setDataSource("http://usa8-vn.mixstream.net:8138");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.stop();
        }
    }
}

in the XML (main.xml) code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Source: (Radio La Chevere)"
        android:layout_marginTop="10dip" android:gravity="center" />
<ProgressBar android:id="@+id/progressBar1"
        android:indeterminateOnly="false" android:progressDrawable="@android:drawable/progress_horizontal"
        android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
        android:minHeight="20dip" android:maxHeight="20dip"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
        android:layout_marginTop="10dip"></ProgressBar>
<LinearLayout android:id="@+id/linearLayout1"
        android:layout_height="wrap_content" android:layout_width="match_parent"
        android:layout_marginTop="20dip" android:gravity="center">
        <Button android:text="Play" android:id="@+id/buttonPlay"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Stop" android:id="@+id/buttonStopPlay"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>

and the android manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="package.your.RadioStream"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".myMain"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

Maveňツ
  • 1
  • 12
  • 50
  • 89
zvzej
  • 6,276
  • 7
  • 33
  • 41
  • progress bar not working but it is streaming could you please take a look what could be issue? and thanks for this streaming tutorial its working for me. – UMAR-MOBITSOLUTIONS Nov 03 '11 at 12:57
  • @zvzej : can you please tell me how can we stream when datasource is not known ?? i mean when we do not have a url to specify on this line `player.setDataSource("http://usa8-vn.mixstream.net:8138");` ,so in this case how can v stream radio..if u know nething then please share .. – Shruti Jul 12 '12 at 10:51
  • @Shruti, well you could ask for it every time you open the aplication from your website. – zvzej Jul 13 '12 at 00:26
  • @ UMAR, yeah I ended deleting the progress bar, since is a continues stream to hard for me to control it. – zvzej Jul 13 '12 at 00:28
  • @zvzej i have one Toggle button to stop & Play for live streaming. so first time i click on play sound play and its working than i click on stop its also working. but when i again click on play it start from initial not from resume . so what can i do to resolve this. ? – Hitarth Oct 03 '12 at 11:27
  • @Coder I would try making a variable that like int change with an initial value of 1 and at pressing stop givin that variable a value of let's say a 2 then in play make an if statement that if is a 2 go to resume not start stream, and if the variable is 1 start stream from beginning. – zvzej Oct 03 '12 at 14:56
  • @zvzej thanx for suggetion. but how can i play audio from resume state ? – Hitarth Oct 04 '12 at 06:04
  • 2
    @Coder you should use a service instead (Player Service) this will give you more options like pausing the stream not stoping it, then you can use resume look into this link http://developer.android.com/guide/topics/media/mediaplayer.html it will help you understand it better. – zvzej Oct 04 '12 at 16:40
  • 1
    @zvzej Thanx . i have done it and your suggetion is help ful to me. – Hitarth Oct 05 '12 at 05:24
  • How can I use this in a fragment please? I tried everything keep getting error messages. – senimii Oct 14 '14 at 08:10
  • @zvzej Hi i got stream url like this and i am playing with your demo project but its not work rtsp://104.41.157.87:1935/videochat/testA1_audio its not audio its video. – Teraiya Mayur Dec 24 '14 at 16:22
  • Replace `player.start()` into `mp.start()` – ajdeguzman Nov 16 '15 at 06:56
  • The progress bar goes and goes on for days but no sound is heard :-( – Nabin Feb 12 '16 at 17:36
4

Well, if you expect a progress bar, you get disapointing, this is a stream, which by default has no time to end is endless. About the URL you can use the mount point in shoutcast2/icecast server and the / in the shoutcast1.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
1

In onResume or wrvr you want! Paste this code.

    String url = "http://server2.crearradio.com:8371"; // your URL here
    final MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
        }
    });

In the manifest.xml add the Internet permission.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Mario Alzate
  • 372
  • 3
  • 8