0

我正在开发音频应用程序,我需要处理从媒体播放器的通知中寻找。我将 setActions(PlaybackState.ACTION_SEEK_TO) 添加到我的播放状态,但它不起作用。如何处理从通知中寻找媒体播放器?怎么做?这是我的 onStartCommand():

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {


        val audio = intent?.getStringExtra("audio")
        val podcastId = intent?.getStringExtra("podcastId")


        

        if(intent?.action == ACTION_INIT){
            stepPlayer.playerInit(audio.toString())
            stepPlayer.setCurrentPlaying(podcastId.toString())
        }
        if(intent?.action == ACTION_PAUSE){
            stepPlayer.playerPause()
        }
        if(intent?.action == ACTION_PLAY){
            stepPlayer.playerPlay()
        }

        mediaSession.setPlaybackState(
            PlaybackState.Builder()
                .setState(
                    PlaybackState.STATE_PLAYING,
                    stepPlayer.mp.currentPosition.toLong(),
                    1.0f,

                )
                .setActions(PlaybackState.ACTION_SEEK_TO)
                .build()
        )


        mediaSession.setMetadata(
            MediaMetadata.Builder()
                .putString(MediaMetadata.METADATA_KEY_TITLE, "TITLE")
                .putString(MediaMetadata.METADATA_KEY_ARTIST, "ARTIST")
                .putLong(MediaMetadata.METADATA_KEY_DURATION, stepPlayer.mp.duration.toLong())
                .build()
        )
        
        return super.onStartCommand(intent, flags, startId)
    }

这里 createNotification():

private fun createNotification(): Notification{
        val playIntent = Intent(this, MediaPlayerService::class.java)
        playIntent.action = ACTION_PLAY
        val playPendingIntent = PendingIntent.getService(this, 0, playIntent, 0)
        val playAction = Notification.Action(R.drawable.ic_media_play,
            "Play",
            playPendingIntent
        )

        val pauseIntent = Intent(this, MediaPlayerService::class.java)
        pauseIntent.action = ACTION_PAUSE
        val pausePendingIntent = PendingIntent.getService(this, 0, pauseIntent, 0)
        val pauseAction = Notification.Action(R.drawable.ic_media_pause,
            "Pause",
            pausePendingIntent)


        val notificationBuilder = Notification.Builder(this)
        //addPlayPauseAction(notificationBuilder)
        notificationBuilder
            .setStyle(Notification.MediaStyle()
                .setMediaSession(mediaSession.sessionToken)
                .setShowActionsInCompactView(0))
            //.addAction(Notification.Action(R.drawable.ic_media_play, "sdw", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PAUSE)))
            .addAction(playAction)
            .addAction(pauseAction)
            .setSmallIcon(R.drawable.ic_media_play)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setUsesChronometer(true)
            .setChannelId("Media channel")
            .setContentTitle("My Title")
            .setContentText("My subtitle")

        setNotificationPlaybackState(notificationBuilder)
        return notificationBuilder.build()
    }
4

0 回答 0