0
连接电源时想播放歌曲,断开电源时要停止播放歌曲,但在播放电源时仍然播放歌曲断开连接。请分享有关此的工作代码。连接电源时播放了歌曲,但断电时不停止
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(context, R.raw.mysong);
if (intent.getAction() == Intent.ACTION_POWER_CONNECTED) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
else if (intent.getAction() == Intent.ACTION_POWER_DISCONNECTED) {
Toast.makeText(context, "POWER DISCONNECTED !!!!!", Toast.LENGTH_SHORT).show();
mMediaPlayer.stop();
}
}
}
你试过调试过吗?电源断开时,是否会调用'mMediaPlayer.stop();'命令行? –
mMediaPlayer.stop(); Toast.makeText(context,“POWER DISCONNECTED !!!!!”,Toast.LENGTH_SHORT).show();即时通讯我得到这个烤面包在我的设备上运行 –