从通知栏停止媒体播放器

Stop Media Player From Notification Bar

我做了一个定时器按钮来设置播放音乐片段的时间。我创建了闹钟管理器来设置时间,

AlarmMAnager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

设置 10 秒

alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime() +
                            10 * 1000, alarmIntent);

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

    private CharSequence msg = "Notofication";
    MediaPlayer mp;
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
        mp = MediaPlayer.create(context, R.raw.music);
        mp.start();

        Intent dismissIntent = new Intent(context,DismissBroadcast.class);
        PendingIntent piDismiss = PendingIntent.getBroadcast(context, 100, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder =
                (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Test")
                        .setAutoCancel(true)
                        .setContentText("test")
                        .setPriority(Notification.PRIORITY_MAX)
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .addAction(R.drawable.ic_cancel_white_36dp,
                                "Dismiss", piDismiss);

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
    public MediaPlayer cancelMusic(){
        return mp;
    }
}

DismissBroadcast.java

public class DismissBroadcast extends BroadcastReceiver {
    AlarmReceiver receive;

    @Override
    public void onReceive(Context context, Intent intent) {

        receive.cancelMusic();
        receive.mp.stop();
        try {
            receive.mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        receive.mp.seekTo(0);
    }
}

当我从 notification 栏点击关闭 button 时,应用程序会崩溃。请给我一些想法如何从 AlarmReceiver.java 启动媒体播放器并从 DismissBroadcast.java 停止它。

这只是一个想法,更多的代码你应该提高自己。

1) 创建一个包含所有控制音乐逻辑的单例class:

public class MusicControl {
    private static MusicControl sInstance;
    private Context mContext;
    private MediaPlayer mMediaPlayer;
    public MusicControl(Context context) {
        mContext = context;
    }

    public static MusicControl getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new MusicControl(context);
        }
        return sInstance;
    }

    public void playMusic() {
        mMediaPlayer = MediaPlayer.create(context, R.raw.music);
        mMediaPlayer.start();
    }

    public void stopMusic() {
        if(mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.seekTo(0);
        }
    }
}

2) AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MusicControl.getInstance(context).playMusic();
        // do your code here...
    }
}

3) DismissBroadcast.java

public class DismissBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MusicControl.getInstance(context).stopMusic();
        // do your code here...
    }
}