Hello stackoverflow community I'm doing an app that plays streaming audio as a service in the background, what I want to do is remove variables from the service from the Activity as well to update the play / pause buttons This is my service code
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import java.io.IOException;
public class PlayRadio extends Service{
private static final String LOGCAT = null;
public MediaPlayer mediaPlayer;
public void onCreate(){
super.onCreate();
Log.d(LOGCAT, "Service Started!");
mediaPlayer = new MediaPlayer();
}
@SuppressLint("WrongConstant")
public int onStartCommand(Intent intent, int flags, int startId){
try {
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("http://localhost:8080/hls/radio.m3u8"));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(LOGCAT, "Media Player started!");
if(!mediaPlayer.isLooping()){
Log.d(LOGCAT, "Problem in Playing Audio");
}
return 1;
}
public void onStop(){
mediaPlayer.stop();
mediaPlayer.release();
}
public boolean isPlaying(){
if(mediaPlayer.isLooping()){
return true;
} else {
return false;
}
}
public void onPause(){
mediaPlayer.stop();
mediaPlayer.release();
}
public void onDestroy(){
mediaPlayer.stop();
mediaPlayer.release();
}
@Override
public IBinder onBind(Intent objIndent) {
return null;
}
What I want is from the activity call isPlaying () so to know if the audio is playing or not and update the buttons
EDITO
This is how I call from the Activity (within the event of the button)
if (!isPLAYING) {
isPLAYING = true;
Intent objIntent = new Intent(getApplicationContext(), PlayRadio.class);
startService(objIntent);
fab.setImageResource(android.R.drawable.ic_media_pause); //Cambia el icono
} else {
isPLAYING = false;
Intent objIntent = new Intent(getApplicationContext(), PlayRadio.class);
stopService(objIntent);
fab.setImageResource(android.R.drawable.ic_media_play); //Cambia el icono
}
The button changes icon from play to play and the music plays quietly When leaving the app it continues playing as a service in the background, the problem is that when you re-enter the app the button is in "Play" and when you click it launches the service that was already running, being that should close it