音频播放器中带前台服务的Notification

官网参考:

https://developer.android.google.cn/media/implement/playback-app?hl=zh-cn#implementing_a_mediasessionservice
https://developer.android.google.cn/develop/ui/views/notifications/expanded?hl=zh-cn

AndroidManifest.xml

<service android:name=".service.MediaPlayerService"
            android:foregroundServiceType="mediaPlayback"></service>  //如果没有notification点击跳转,就不需要这句

Service中:

注意:增加builder.setCustomBigContentView(mNotificationView) !
           去除.setSmallIcon(R.drawable.ic_notification_recording)!

 private void setNotification () {
        if (mItem == null) return;
        mNotificationView = new RemoteViews(getPackageName(), R.layout.notification);
        mNotificationView.setTextViewText(R.id.app_name, getResources().getString(R.string.app_name));
        mNotificationView.setTextViewText(R.id.file_name, mItem.getFileName());
        String action = null;
        if (mIntent != null) action = mIntent.getAction();
        changeNotification(action);
        if (action.equals(ACTION_STOP)) return;

        Intent intent = null;
        PendingIntent pendingIntent = null;

        intent = new Intent(this, MediaPlayerService.class);
        intent.setAction(ACTION_GOON_PLAY);
        pendingIntent = PendingIntent.getService(this, 0, intent, FLAG_MUTABLE);
        mNotificationView.setOnClickPendingIntent(R.id.btn_goon_play, pendingIntent);

        intent = new Intent(this, MediaPlayerService.class);
        intent.setAction(ACTION_PAUSE);
        pendingIntent = PendingIntent.getService(this, 0, intent, FLAG_MUTABLE);
        mNotificationView.setOnClickPendingIntent(R.id.btn_pause, pendingIntent);

        intent = new Intent(this, MediaPlayerService.class);
        intent.setAction(ACTION_STOP);
        pendingIntent = PendingIntent.getService(this, 0, intent, FLAG_MUTABLE);
        mNotificationView.setOnClickPendingIntent(R.id.btn_stop, pendingIntent);

        Bundle bundle = new Bundle();
        bundle.putSerializable("ITEM", mItem);
        intent = new Intent(this, MediaActivity.class);
        intent.putExtras(bundle);
        pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        Notification notification = new Notification();
        notification.contentView = mNotificationView;
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        notification.icon = R.drawable.ic_notification_recording;
        notification.contentIntent = pendingIntent;

        setChannel();
        Notification.Builder builder = new Notification.Builder(mContext, notification)
                .setSmallIcon(R.drawable.ic_notification_recording)
                .setChannelId(channelId)
                .setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
        notification = builder.build();
        startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);

        /*mBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_notification_recording)
                .setCustomContentView(mNotificationView);

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel (channelId, getResources().getString(R.string.app_name),
                NotificationManager.IMPORTANCE_LOW);
        mNotificationManager.createNotificationChannel(channel);
        Notification notification = mBuilder
                .setContentIntent(pendingIntent).build();

        mNotificationManager.notify(notificationID, notification);*/

    }

    private void setChannel () {
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel (channelId, getResources().getString(R.string.app_name),
                NotificationManager.IMPORTANCE_DEFAULT);
        if (channel == null) {
            channel = new NotificationChannel(channelId,
                    getResources().getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setShowBadge(false);
            channel.enableLights(false);
            channel.enableVibration(false);
            channel.setBypassDnd(true);
            channel.setSound(null,null);
            mNotificationManager.createNotificationChannel(channel);
        }
    }

    private void cancelNotification () {
        mItem = null;
        if (mNotificationManager != null) {
            /*mNotificationManager.cancel(notificationID);*/
            stopForeground(true);
            mNotificationManager = null;
        }
    }
    
    private void changeNotification (String action) {
        if (action == null) return;
        if (action.equals(ACTION_GOON_PLAY)) {
            mNotificationView.setViewVisibility(R.id.btn_pause, View.VISIBLE);
            mNotificationView.setViewVisibility(R.id.btn_goon_play,View.GONE);
            mNotificationView.setViewVisibility(R.id.btn_stop,View.VISIBLE);
            if (mMediaPlayerUtil != null && mMediaPlayerUtil.getmPlayerState() != MediaState.PLAYING) {
                mMediaPlayerUtil.mediaPlayerStart();
            }
        } else if (action.equals(ACTION_PAUSE)) {
            mNotificationView.setViewVisibility(R.id.btn_pause,View.GONE);
            mNotificationView.setViewVisibility(R.id.btn_goon_play,View.VISIBLE);
            mNotificationView.setViewVisibility(R.id.btn_stop,View.VISIBLE);
            if (mMediaPlayerUtil != null && mMediaPlayerUtil.getmPlayerState() != MediaState.PAUSE) mMediaPlayerUtil.mediaPlayerPause();
        } else if (action.equals(ACTION_STOP)) {
            cancelNotification();
            if (mMediaPlayerUtil != null && mMediaPlayerUtil.getmPlayerState() == MediaState.PLAYING) {
                mMediaPlayerUtil.mediaPlayerStop();
            }
        } else if (action.equals(ACTION_RESTART)) {
            mNotificationView.setViewVisibility(R.id.btn_pause, View.VISIBLE);
            mNotificationView.setViewVisibility(R.id.btn_goon_play,View.GONE);
            mNotificationView.setViewVisibility(R.id.btn_stop,View.VISIBLE);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值