Stop Media Player From Notification Bar
<p>I make a timer button to set the time to play the music clip. I create the Alarm Manager to set the time,</p>

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

<p>Set 10sec </p>

<pre><code>alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    SystemClock.elapsedRealtime() +
                            10 * 1000, alarmIntent);
</code></pre>

<p>AlarmReceiver.java</p>

<pre><code>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;
    }
}
</code></pre>

<p>DismissBroadcast.java</p>

<pre><code>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);
    }
}
</code></pre>

<p>When I click the dismiss <code>button</code> from <code>notification</code> bar, the app will crash. Please give me some ideas how to start the media player from <code>AlarmReceiver.java</code> and stop it from <code>DismissBroadcast.java</code>.</p>