停止从BroadcastReceiver启动的Android Media Player循环

问题描述 投票:0回答:1

我有一个BroadcastReceiver,可以启动MediaPlayer并循环播放声音。我的问题是,当应用程序处于后台并且接收器被触发时,播放器会播放声音。现在,当我再次打开应用程序时,我想停止播放器。我试图在活动的onResume中向接收方发送消息,但播放器的实例为null。启动/重新打开应用程序时如何停止播放器?

android broadcastreceiver android-mediaplayer android-lifecycle
1个回答
0
投票

尝试从Service控制MediaPlayer并使用Context.startService()和Context.stopService()并在其生命周期中处理MediaPlayer

使用此意图:

final Intent mpServiceIntent = new Intent(context, MPService.class);

当您的BroadcastReceiver被触发时,使用:

startService(mpServiceIntent);

[调用Activity.onCreate()时使用:

stopService(mpServiceIntent);
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.