0

I am trying to play audio from this stream (take note that this is NOT A FILE) https://listen.radioaktywne.pl:8443/ramp3 , unfortunately with no luck whatsoever. When copy-pasting this link to my web browser it plays normally the online radio.

I have tried messing with setAudioStreamType() and AudioAttributes but none of the above seem to work. Also when I try a URL to a simple mp3 file everything works fine. Any suggestions?

This is the entire MainActivity.java file with the key part being just initializeMediaPlayer() in my opinion.

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    // The audio url to play
    String audioUrl = "https://listen.radioaktywne.pl:8443/ramp3";
    private FloatingActionButton fab;
    private boolean playing=false;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initializeMediaPlayer();

        fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, R.string.now_listening, Snackbar.LENGTH_LONG).show();
                if (!playing) {
                    startPlaying();
                } else {
                    stopPlaying();
                }
            }
        });

    }
    private void stopPlaying() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            initializeMediaPlayer();
            playing=false;
        }

    }
    private void startPlaying() {
        mediaPlayer.prepareAsync();
        playing =true;

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

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

    }
/**
* Take a look here!
*/
    private void initializeMediaPlayer() {
          mediaPlayer = new MediaPlayer();
//        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//        AudioAttributes audioAttributes = new AudioAttributes.Builder()
//                .setUsage(AudioAttributes.USAGE_MEDIA)
//                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
//                .build();
//
//        mediaPlayer.setAudioAttributes(audioAttributes);
        try {
            mediaPlayer.setDataSource(audioUrl);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • Possible duplicate of [how to play audio file from url in android](https://stackoverflow.com/questions/5974392/how-to-play-audio-file-from-url-in-android) – James Conway Jun 14 '19 at 19:33
  • @JamesConway As I mentioned in the title, this is not about playing an audio file from URL. If you use the above code to play an audio file from URL it works just fine. I have a problem with the type of link, which does not point to any specific file. I am pretty sure it requires some configuration of the `MediaPlayer` class – Tomek Szypuła Jun 14 '19 at 19:59

0 Answers0