android-mediaplayer - 我如何处理媒体播放器通知中的搜索?
问题描述
我正在开发音频应用程序,我需要处理从媒体播放器的通知中寻找。我将 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()
}
解决方案
推荐阅读
- javascript - Chrome 扩展 url 块过滤器不获取动态数据
- php - 对具有某些已知值优先级的数组进行排序,php
- django - 将用户重定向到上一页django
- python - ModuleNotFoundError:在heroku中在线运行django应用程序时没有名为“mysite”的模块
- swift - CallKit reportNewIncomingCall 完成调用,但仍然得到“杀死应用程序,因为它从未发布来电......”崩溃
- javascript - Swiper 拇指滑块不工作
- firebase - 如何使用颤振将带有标题和描述的视频上传到 Firebase?
- google-maps - Google在quasar vue问题中自动填充没有地图的地址
- python - 直方图在 python 中不显示任何内容
- dialogflow-es - 如何在对话流中向系统实体添加同义词?