Hello, I wanted to know how to make the stream continue to run, even if I went to another screen, or quit the application. But I do not know how. Someone give me a hint and example. Thanks
I'm using class of type Fragmen When I left for another activity I already saw in the log this: W / MediaPlayer-JNI: MediaPlayer finalized without being released
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1aovivo, container, false);
imgStream = (ImageView) view.findViewById(R.id.imgStreamId);
btnTocar = (ImageView) view.findViewById(R.id.btnExecutarId);
btnTocar.setImageResource(R.drawable.play);
imgStream.setVisibility(imgStream.INVISIBLE);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
progressDialog = new ProgressDialog(getActivity());
btnTocar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(estadoInicial) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
estadoInicial = false;
} else {
if(playPause) {
new Player().execute(stream);
} else {
if(!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
estadoInicial = true;
playPause = false;
}
checkIsPlaying();
}
});
checkIsPlaying();
return view;
}
private void checkIsPlaying(){
if(!estadoInicial) {
btnTocar.setImageResource(R.drawable.play);
imgStream.setImageResource(R.drawable.streaming_parado);
} else {
btnTocar.setImageResource(R.drawable.stop);
Glide.with(getActivity()).load(R.drawable.streaming).asGif().into(imgStream);
}
}
class Player extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
estadoInicial = false;
playPause = true;
btnTocar.setImageResource(R.drawable.play);
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
prepared = false;
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if(progressDialog.isShowing()) {
progressDialog.cancel();
}
btnTocar.setImageResource(R.drawable.stop);
mediaPlayer.start();
imgStream.setVisibility(imgStream.VISIBLE);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Glide.with(getActivity()).load(R.drawable.carregando).asGif().into(btnTocar);
progressDialog.setMessage("Carregando Streaming...");
progressDialog.show();
}
}
@Override
public void onPause() {
super.onPause();
Toast.makeText(getActivity(), "atc pause", Toast.LENGTH_SHORT).show();
if(estadoInicial) {
mediaPlayer.start();
Toast.makeText(getActivity(), "play-pause", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onResume() {
super.onResume();
if(estadoInicial) {
mediaPlayer.start();
}
}
@Override
public void onDestroy() {
if(prepared) {
mediaPlayer.release();
mediaPlayer = null;
}
super.onDestroy();
}