0

I'm trying to implement a simple radio streaming app based on one stream URL. However when running the app, it doesn't seem to work.

The application prompts the user to wait until the stream is ready, then the play button is to be enabled.

Can someone provide some insight into this?

Is the issue with the stream URL or the code itself? I've used to the following video to guide me through this process: https://www.youtube.com/watch?v=pPpVZ8YZXHk

package com.example.oradio;

import androidx.appcompat.app.AppCompatActivity;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private Button Play;
MediaPlayer player;
private boolean prepared = false;
private boolean started = false;
String[] links = {"http://stream.radioreklama.bg/radio1rock64",
                    "http://stream.radioreklama.bg/radio1rock128",
                    "http://stream.radioreklama.bg/city128",
                    "http://stream.radioreklama.bg/nrj128"};

String hop = "http://stream.radioreklama.bg:80/radio1rock128";


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

    Play = (Button) findViewById(R.id.btnPlay);
    Play.setEnabled(false);
    Play.setText("Please wait..");


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


    new PlayerTask().execute(hop);

    Play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(started) {
                started = false;
                player.pause();
                Play.setText("PLAY");
            }
            else {
                started = true;
                player.start();
                Play.setText("PAUSE");
            }

        }
    });
}
class PlayerTask extends AsyncTask<String, Void, Boolean>{
    @Override
    protected Boolean doInBackground(String... strings) {

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

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        Play.setEnabled(true);
        Play.setText("PLAY");
    }
    }

@Override
protected void onPause() {
    super.onPause();
    if(started) {
        player.pause();
    }
}

@Override
protected void onResume() {
    super.onResume();
    if(started) {
        player.start();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if(prepared) {
        player.release();
    }
}
}

The following permissions have been added to the Manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Sapian Jr
  • 1
  • 1
  • You need to allow your app to use insecure HTTP traffic: https://stackoverflow.com/questions/51902629/how-to-allow-all-network-connection-types-http-and-https-in-android-9-pie – Alexander Hoffmann Nov 10 '21 at 21:42

0 Answers0