如何在 Android 的 MainActivity 中暂停/播放背景音乐?
Posted
技术标签:
【中文标题】如何在 Android 的 MainActivity 中暂停/播放背景音乐?【英文标题】:How to pause/play background music in MainActivity in Android? 【发布时间】:2019-03-23 17:04:49 【问题描述】:我正在 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】:不确定问题中的代码是否不完整,但您需要在服务中调用 registerReceiver 以便接收器实际接收广播。
另外,如果这主要是在同一进程中进行通信,我建议使用LocarBroadcastManager,因为它更高效、更安全。
【讨论】:
是的,缺少代码,您的回答对我帮助很大!它现在正在工作以上是关于如何在 Android 的 MainActivity 中暂停/播放背景音乐?的主要内容,如果未能解决你的问题,请参考以下文章