在Android中管理媒体播放器通知

时间:2019-08-01 09:38:56

标签: android android-mediaplayer media-player

我已经能够从服务器流式传输音频,并且还能够在音频开始播放时显示通知控制,但是现在通知通常什么都没有。我现在的问题是如何获取音频标题并将其显示在通知中,所以我尝试了这段代码

loadData: async function(idCode) {
    const responseObject = await axios({
        method: 'post',
        url: 'controllerShowcaseLoadDataWithPromises',
        data: {
            action: 'loadData',
            idCode: idCode
        }
    });
    const response = qsys.getResponse(responseObject);
    this[idCode] = response.data.message;
},

我尝试在启动通知服务时将歌曲的标题发送到Intent intent = new Intent( getApplicationContext(), NotificationService.class ); intent.setAction(STARTFOREGROUND_ACTION ); intent.putExtra("title",title); startService( intent ); (歌曲是根据我代码中项目的标题从服务器获取的),但是我无法在其中接收它NotificationService.class

这是我的NotificationService.class,也是我尝试接收意图的方式,但它会返回NotificationService.class

null

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码创建媒体通知

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
            R.drawable.image4); //replace with your own image


    // Create a new Notification

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this, CHANNEL_ID)
            // Hide the timestamp
            .setShowWhen(false)
            // Set the Notification style
            .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                    // Attach our MediaSession token
                    .setMediaSession(mediaSession.getSessionToken())
                    // Show our playback controls in the compat view
                    .setShowActionsInCompactView(0, 1, 2))
            // Set the Notification color
            .setColor(getResources().getColor(R.color.colorAccent))
            // Set the large and small icons
            .setLargeIcon(largeIcon)
            .setSmallIcon(android.R.drawable.stat_sys_headset)
            // Set Notification content information
            .setContentText("activeAudio.getArtist()")
            .setContentTitle("activeAudio.getAlbum()")
            .setContentInfo("activeAudio.getTitle()")
            // Add playback actions
            .addAction(R.drawable.ic_replay_10_black_24dp, "previous", playbackAction(3))
            .addAction(notificationAction, "pause", play_pauseAction)
            .addAction(R.drawable.ic_forward_10_black_24dp, "next", playbackAction(2));

    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notificationBuilder.build());