Remove variables from an android service

1

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

    
asked by Marcos Alexis Barrios Barreto 08.05.2018 в 03:02
source

1 answer

1

If the Service can run in the background, you probably start and stop it with a Intent :

Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
startService(myService);

Therefore you do not have an instance to call the isPlaying () method that you comment; to determine when to change the text of the buttons, it should be done when you stop the service, change the text of the buttons and from the text you would know if it is activated or stopped, check this example .

    
answered by 08.05.2018 в 17:34