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
.
App from them,
This failure does not occur.
What do I need to do so my player does not stop either by minimizing App ?
Note: The App's Radios and Young Pan radios also stop while 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();
}
}
}