androidbroadcastreceiverandroid-serviceplaybackbackground-music

How to pause/play background music in MainActivity in Android?


I'm calling the service MusicaFundo in Splashscreen.class and want to pause/play it in MainActivity.class. I'm trying with a sendBroadcast but the service is not receiving the intent, I have created the BroadcastReceiver, MyServiceReceiver, inside MusicaFundo.class, is it wrong to do like this?

MainActivity.class

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    boolean musicaTocar = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent svc = new  Intent (this, MusicaFundo.class);

        TextView titulo = findViewById(R.id.titulo);
        final ImageView som = findViewById(R.id.som);
        som.setBackgroundResource(R.drawable.volumeup);


        final ImageView jogomem = findViewById(R.id.jogomem);

        jogomem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        Animation diminuir = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.diminuir);
        jogomem.startAnimation(diminuir);
        Intent jogomemoria = new Intent(MainActivity.this, JogoMemoria.class);
        startActivity(jogomemoria);

            }
        });


        Animation deslocarD = (Animation) AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        titulo.startAnimation(deslocarD);



        som.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!musicaTocar){
                    som.setBackgroundResource(R.drawable.volumemut);
                    playMusica();
                    musicaTocar = true;
                }
                else {
                    if (musicaTocar){
                        som.setBackgroundResource(R.drawable.volumeup);
                        pausarMusica();
                        musicaTocar = false;
                    }
                }
            }
        });



    }

    private void pausarMusica() {
        Intent pausar = new Intent();
        pausar.setAction("pausar");
        sendBroadcast(pausar);
    }

    private void playMusica() {
        Intent tocar = new Intent();
        tocar.setAction("tocarmusica");
        sendBroadcast(tocar);
    }

Inside MusicaFundo.class

 public class MusicaFundo extends Service {
        private MediaPlayer player;
        public int posatual = 0;
        private static final String TAG = "MusicaFundo";

        public static Object getName() {
            return null;
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate(){
            player = MediaPlayer.create(this, R.raw.musicafundo);
            player.setLooping(true);
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            player.seekTo(posatual);
            player.start();
            return START_STICKY;

        }

         public boolean onUnbind(Intent intent){
             return false;
         }



        @Override
        public void onDestroy() {
            super.onDestroy();
            if (player != null) {
                if (player.isPlaying()) {
                    player.stop();
                }
                player.release();
            }
        }


        public void resumeMusic(){
            if (player != null) {
                if (player.isPlaying() == false) {
                    player.seekTo(posatual);
                    player.start();
                }
            }
        }

        public void onPause(){
            if (player != null) {
                if (player.isPlaying()) {
                    posatual = player.getCurrentPosition();
                    player.pause();
                }
            }
        }  

     public class MyServiceReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {  
               String pausa = intent.getAction();

                if(pausa.equals("pausar")){
                    onPause();
                    Log.d(TAG, "onReceive: " + pausa);
                }
                else if(pausa.equals("tocarmusica")){
                    resumeMusic();
                    Log.d(TAG, "onReceive: " + pausa);

                }
            }
        }

Want to receive intent so it compares the string to pause or play.


Solution

  • Not sure if the code is just not complete in the question but you need to call registerReceiver in your service for the receiver to actually receive broadcasts.

    Also, if this is mostly communication within the same process, I suggest using LocarBroadcastManager as it is more efficient and secure.