从通知栏停止媒体播放器

时间:2016-11-17 08:06:41

标签: android broadcastreceiver android-service alarmmanager android-broadcast

我制作了一个计时器按钮来设置播放音乐片段的时间。我创建了Alarm Manager来设置时间,

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);
    }
}

当我点击button栏中的notification时,应用程序将崩溃。请告诉我如何从AlarmReceiver.java启动媒体播放器并将其从DismissBroadcast.java停止。

1 个答案:

答案 0 :(得分:2)

这只是一个想法,对于更多代码,你应该自己提升。

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

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...
    }
}