Radio Player Android Parando

Asked

Viewed 34 times

0

So I have the following Activity below.

In it, I have a radio player.

However, when I minimize the App to see something else, such as using the calculator, the player stops playing.

I’m using the streaming of Rádio Capital UK.

In the App theirs,

https://play.google.com/store/apps/details?id=com.thisisglobal.player&hl=en_GB

this failure does not occur.

What needs to be done so that my player also does not stop when minimizing the App?

Note: The App’s of Radios Transamerica and Young Pan also stop when minimizing!

package carcleo.com.radiosingular;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.io.IOException;

public class principal extends AppCompatActivity {


    private String url_radio= "rtsp://cdn-the-2.musicradio.com:80/LiveAudio/Capital";
    private ProgressBar playSeekBar;
    private MediaPlayer player;

    private TextView tvRadioUrl;
    private Button buttonPlay;
    private Button buttonStop;

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

        initializeUIElements();
        initializeMediaPlayer();
    }


    private void initializeUIElements() {

        playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
        buttonPlay  = (Button)      findViewById(R.id.buttonPlay);
        buttonStop  = (Button)      findViewById(R.id.buttonStop);
        tvRadioUrl  = (TextView)    findViewById(R.id.textViewRadioUrl);

        playSeekBar.setMax(100);
        playSeekBar.setVisibility(View.INVISIBLE);
        playSeekBar.setIndeterminate(true);

        buttonStop.setEnabled(true);
        buttonPlay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                startPlaying();
            }
        });

        buttonStop.setEnabled(false);
        buttonStop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                stopPlaying();
            }
        });

        tvRadioUrl.setText("Radio url : "+url_radio);
    }

    public void onClick(View v) {
        if (v == buttonPlay) {
            startPlaying();
        } else if (v == buttonStop) {
            stopPlaying();
        }
    }

    private void startPlaying() {
        buttonStop.setEnabled(true);
        buttonPlay.setEnabled(false);

        playSeekBar.setVisibility(View.VISIBLE);

        player.prepareAsync();

        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {

                player.start();

            }
        });

    }

    private void stopPlaying() {
        if (player.isPlaying()) {
            player.stop();
            player.release();
            initializeMediaPlayer();
        }

        buttonPlay.setEnabled(true);
        buttonStop.setEnabled(false);
        playSeekBar.setIndeterminate(true);
        playSeekBar.setVisibility(View.INVISIBLE);

    }

    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        try {
            player.setDataSource(url_radio);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setIndeterminate(false);
                playSeekBar.setSecondaryProgress(100);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.pause();
        }
    }
}
  • Hello, you have to make your application run in the background all the time, so it will not stop running and the streaming is always active. Here is a link, which talks about what you need ! https://medium.com/android-dev-br/workingcom-tasks-em-background-3d4da889ddfa

  • Using Assynctasck?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.