我正在做一个包含主要活动和2个服务的声音应用程序,在mainActivity中我可以控制是否通过主要活动上的按钮手动播放或暂停音乐(所以你可以在屏幕上看到它)启动一个激活/停止媒体播放器的服务。此外,我有一个媒体播放器控制通知在其他服务中控制播放/停止在后台。问题是我需要在后台使用主要活动的主要控件来协调通知,例如当我放入锁屏(播放音乐时)并按下通知上的停止时,音乐不会停止因为在主要活动中,媒体播放器的服务继续启用。因此,我需要知道通知是否在主要活动中播放音乐以进行布尔值(如果通知停止音乐,我希望de mediaplayer服务停止播放音乐)。 谢谢你们! 这是我的通知代码:
public class Notificaciones extends Service {
public static final String ACTION_PLAY = "action_play";
public static final String ACTION_PAUSE = "action_pause";
public static final String ACTION_REWIND = "action_rewind";
public static final String ACTION_FAST_FORWARD = "action_fast_forward";
public static final String ACTION_NEXT = "action_next";
public static final String ACTION_PREVIOUS = "action_previous";
public static final String ACTION_STOP = "action_stop";
public static final String ACTION_BAR = "action_bar";
private MediaSession mSession;
private MediaSessionManager mManager;
private MediaController mController;
private MediaPlayer mMediaPlayer;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mManager == null) {
mMediaPlayer = new MediaPlayer();
mSession = new MediaSession(getApplicationContext(), "example");
mController = new MediaController(getApplicationContext(), mSession.getSessionToken());
mSession.setCallback(new MediaSession.Callback() {
@Override
public void onPlay() {
super.onPlay();
Log.e("MyServiceClass", "onPlay");
buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
}
@Override
public void onPause() {
super.onPause();
Log.e("MyServiceClass", "onPause");
buildNotification(generateAction(android.R.drawable.ic_media_play, "Play", ACTION_PLAY));
}
@Override
public void onSkipToNext() {
super.onSkipToNext();
buildNotification(generateAction(android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE));
}
@Override
public void onFastForward() {
super.onFastForward();
}
@Override
public void onRewind() {
super.onRewind();
}
@Override
public void onStop() {
super.onStop();
Log.e("MyServiceClass", "onStop");
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
Intent intent = new Intent(getApplicationContext(), MyServiceClass.class);
stopService(intent);
}
});
}
handleIntent(intent);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
Log.e("MyServiceClass", "onStop");
mController.getTransportControls().stop();
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
Intent intent = new Intent(getApplicationContext(), MyServiceClass.class);
stopService(intent);
mMediaPlayer.stop();
mMediaPlayer.release();
stopSelf();
super.onDestroy();
}
private void handleIntent (Intent intent){
if (intent == null || intent.getAction()==null){
return;
}
String action = intent.getAction();
if (action.equalsIgnoreCase(ACTION_PLAY)){
mController.getTransportControls().play();
}else if (action.equalsIgnoreCase(ACTION_PAUSE)){
mController.getTransportControls().pause();
}else if (action.equalsIgnoreCase(ACTION_FAST_FORWARD)){
mController.getTransportControls().fastForward();
}else if (action.equalsIgnoreCase(ACTION_REWIND)){
mController.getTransportControls().rewind();
}else if (action.equalsIgnoreCase(ACTION_PREVIOUS)){
mController.getTransportControls().skipToPrevious();
}else if (action.equalsIgnoreCase(ACTION_NEXT)){
mController.getTransportControls().skipToNext();
}else if (action.equalsIgnoreCase(ACTION_STOP)){
mController.getTransportControls().stop();
}if (action.equalsIgnoreCase(ACTION_BAR)){
mController.getTransportControls().stop();
}
}
private android.app.Notification.Action generateAction(int icon, String title, String intentAction){
Intent intent = new Intent(getApplicationContext(), MyServiceClass.class);
intent.setAction(intentAction);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
return new android.app.Notification.Action.Builder(icon, title, pendingIntent).build();
}
private void buildNotification(android.app.Notification.Action action){
android.app.Notification.MediaStyle style = new android.app.Notification.MediaStyle();
Intent intent = new Intent(getApplicationContext(), MyServiceClass.class);
intent.setAction(ACTION_STOP);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
android.app.Notification.Builder builder = new android.app.Notification.Builder(this).setSmallIcon(R.drawable.logo).setContentTitle("TEEN FM").setContentText("Adolescents.cat")
.setDeleteIntent(pendingIntent).setStyle(style);
builder.addAction(generateAction(android.R.drawable.ic_media_pause, "Previous", ACTION_PREVIOUS));
builder.addAction(generateAction(android.R.drawable.ic_media_rew, "Rewind", ACTION_REWIND));
builder.addAction(action);
builder.addAction(generateAction(android.R.drawable.ic_media_ff, "Fast Forward", ACTION_FAST_FORWARD));
builder.addAction(generateAction(android.R.drawable.ic_media_next, "Next", ACTION_NEXT));
style.setShowActionsInCompactView(0, 1, 2, 3, 4);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
@Override
public boolean onUnbind(Intent intent) {
mSession.release();
return super.onUnbind(intent);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
答案 0 :(得分:0)
您应该在MainActivity中将MediaPlayer声明为静态对象,然后从Notificaciones服务中调用它。
private static MediaPlayer mMediaPlayer;
MainActivity main;
当你想要停止时,你应该做下一件事:
main=new MainActivity();
main.mMediaPlayer.stop
//这是一个例子,我不知道你的玩家是如何工作的。因为如果您正在为MediaPlayer创建一个实例,那么您永远不会从服务中停止它。 MediaPlayer服务是一个实例,而Activity MediaPlayer是另一个实例。
如果您需要更多帮助我。让我知道。祝你有愉快的一天!