yat0
yat0

Reputation: 967

Android: Unable to create MediaPlayer

I'm trying to run a simple test app to stream music from an online shoutcast radio but I'm always getting an error about MediaPlayer setDataSource() method.. This is the log:

08-26 11:33:42.268  17698-17698/com.example.bruno.radioteste D/MainActivity﹕ Attempt to play http://50.7.98.106:8398
08-26 11:33:42.268  17698-17698/com.example.bruno.radioteste E/MediaPlayer﹕ Unable to create media player
08-26 11:33:42.268  17698-17698/com.example.bruno.radioteste W/System.err﹕ java.io.IOException: setDataSource failed.: status=0x80000000
08-26 11:33:42.278  17698-17698/com.example.bruno.radioteste W/System.err﹕ at android.media.MediaPlayer._setDataSource(Native Method)

The code is very simple, and from what I've been searching it should work just fine.. The layout's xml:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="BassDrive-http://50.7.98.106:8398"
    android:onClick="play" />

And the activity class:

 public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener {

private static final String TAG = MainActivity.class.getSimpleName();

private MediaPlayer player;
private boolean isPlaying = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    player = new MediaPlayer();
    player.setOnPreparedListener(this);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
}

public void play(View view) {

    if(isPlaying) {
        player.stop();
        isPlaying = false;
    } else {

        try {

            String stationIp = ((Button)view).getText().toString().split("-")[1];
            Log.d(TAG, "Attempt to play " + stationIp);
            player.setDataSource(stationIp);
            player.prepareAsync();
            isPlaying = true;

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}

@Override
public void onPrepared(MediaPlayer mp) {
    Log.d(TAG, "Trying to start...");
    mp.start();
}

@Override
protected void onStop() {
    super.onStop();

    // clear media player
    player.stop();
    player.release();
    player = null;
}

Upvotes: 0

Views: 316

Answers (1)

Santhosh
Santhosh

Reputation: 3991

There are two possible cases,

Have you added this permission in your manifest?

<uses-permission android:name="android.permission.INTERNET" />

If yes, then android is not able to decode your stream. To test this you can try some other stream. For example, try a spotify 30-second preview stream http://d318706lgtcm8e.cloudfront.net/mp3-preview/d9d8686acc79916d2db76abdaa2c18a594fed332

Upvotes: 3

Related Questions