2

I've been trying so hard to develop an Internet radio Streaming app, but all I get it some force closes or errors. Steps I've taken,

  • I've read the MediaPlayer Documentation here.
  • I've also read the already available solutions here.
  • I've also searched google for any solutions but found none.

Here is my code:

MainActivity:

    private Button pbutton;
    private MediaPlayer mediaPlayer;

    pbutton = findViewById(R.id.button_play);
    pbutton.setEnabled(false);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource("http://streams.abidingradio.org:7800/1");
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.prepareAsync();

    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            pbutton.setEnabled(true);
        }
    });

    pbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaPlayer.start();
        }
    });

Manifest:

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

When I run this code, MediaPlayer is not getting prepared. And it throws the following errors: click here for the full log the main errors are:

failed to init data from source
MediaPlayerNative: error (1, -2147483648)
MediaPlayer: Error (1,-2147483648)

I've been dealing with this problem for almost three days but still haven't found a solution. I can also assure that the link of the audio stream is working perfectly.

Any help would be greatly appreciated!

Thank you.

Brad
  • 159,648
  • 54
  • 349
  • 530
Jerome Marshall
  • 323
  • 1
  • 4
  • 14

4 Answers4

2

The cause is MediaPlayer doesn't support streaming MPEGA format. Take a look into supported audio formats.

Just use one of third-party players, which are compatible with, like IjkPlayer or ExoPlayer

Onix
  • 662
  • 3
  • 10
  • Thanks for your suggestion. But I don't think the issue is with the audio stream. Because, I used the same audio stream link in another sample app from GitHub and it worked fine. But when I use it here this happens. And BTW how would get to know the format of the audio stream. – Jerome Marshall Nov 21 '18 at 13:50
  • "in another sample app from GitHub and it worked fine" - could i have a look on sample? "And BTW how would get to know the format of the audio stream" - Just open stream in any player and see 'video details' (vlc, mediplayer classic) – Onix Nov 21 '18 at 14:03
  • Here is the sample that I used [link](https://github.com/googlesamples/android-MediaBrowserService/). To make it work, I just changed the dataSource to the stream link. – Jerome Marshall Nov 21 '18 at 14:21
  • Thank you so much @Onix. I gave ExoPlayer a try and it works! Now there are new errors like "getDiskStats failed with result NOT_SUPPORTED and size 0" but it still streams the audio. – Jerome Marshall Nov 21 '18 at 16:09
1

Use like this.

Declare:

Uri alarmSound_carHorn;
Ringtone ringtone_carHorn;

Initialize (onCreate):

alarmSound_carHorn = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +  getApplication().getApplicationContext().getPackageName() + "/" +  R.raw.car_horn);
ringtone_carHorn = RingtoneManager.getRingtone(getApplicationContext(),alarmSound_carHorn);

When calling:

if(ringtone_carHorn!= null && !ringtone_carHorn.isPlaying()){
    ringtone_carHorn.play();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
DSalomon
  • 39
  • 1
  • 3
0

I had the same error, but it was only on a few phones and I had no idea how to fix it. I ended up using ExoPlayer

player?.apply {
        val mediaItem: MediaItem = MediaItem.fromUri(url)
        setMediaItem(mediaItem)
        prepare()
        play()
}
midFang
  • 96
  • 3
0

I got the same error only when onPause -> onResume was called. It was caused by the setOnPreparedListener. Solution:

binding?.playbackVideoView?.setOnPreparedListener(null)

clear on prepared listener so it doesn't use the old mediaPlayer object

Kallos Zsolty
  • 211
  • 3
  • 5