如何在Android的MainActivity中暂停/播放背景音乐?

时间:2019-03-23 17:04:49

标签: android broadcastreceiver android-service playback background-music

我在Splashscreen.class中调用服务MusicaFundo,并想在MainActivity.class中暂停/播放它。 我正在尝试使用sendBroadcast,但服务未收到意图,我在MusicaFundo.class内创建了BroadcastReceiver,MyServiceReceiver,这样做是错误的吗?

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);
    }

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);

                }
            }
        }

想要接收意图,因此它将字符串比较为暂停还是播放。

1 个答案:

答案 0 :(得分:1)

不确定问题中的代码是否还不完整,但是您需要在服务中调用registerReceiver,接收者才能实际接收广播。

此外,如果主要是同一流程中的通信,我建议使用LocarBroadcastManager,因为它更有效,更安全。