Livestream link MediaPlayer Android

1

I'm trying to get this stream to play:

    MediaPlayer mp = new MediaPlayer();
    try {
        mp.setDataSource("http://knhc-ice.streamguys1.com/live");
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.prepareAsync();
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
            @Override
            public void onPrepared(MediaPlayer mp)
            {
                mp.start();
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }

But when the application runs it is giving me this error:

2019-03-17 17:01:05.035 5924-5924/com.example.android.c895 W/System.err: java.io.IOException: setDataSource failed.: status=0x80000000

I understand that the link that I am passing into the media player is just a single player, but I want that player to automatically play and be passed to the MediaPlayer. Is there anyway I can do this?

java
android
asked on Stack Overflow Mar 18, 2019 by beastlyCoder • edited Mar 18, 2019 by beastlyCoder

1 Answer

1

What I was able to figure out was put my MediaPlayer on Async Task(background thread) on my application.

mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        b = (ImageButton) bottomSheet.findViewById(R.id.imageButton);

        new PlayerTask().execute(s);

        b.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view)
            {
                if(started)
                {
                    mediaPlayer.start();
                }
            }
        }); 
class PlayerTask extends AsyncTask<String, Void, Boolean>
{

    @Override
    protected Boolean doInBackground(String... strings) {


        try
        {
            mediaPlayer.setDataSource(strings[0]);
            mediaPlayer.prepare();
            prepared = true;
        } catch(IOException e)
        {
            e.printStackTrace();
        }

        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean)
    {
        super.onPostExecute(aBoolean);
        mediaPlayer.start();
    }
}

Since the codes are almost identically the same, could anyone answer why this works and not just on the main thread?

answered on Stack Overflow Mar 18, 2019 by beastlyCoder

User contributions licensed under CC BY-SA 3.0