Commit 8cc2f486 by 刘鹏

feat: PlayService解绑

parent c072cf3b
......@@ -14,7 +14,7 @@ buildscript {
ydlrouter_version = '1.2.3'
constrait_support_version = '1.0.2'
componentVersion = "0.3.0.6-SNAPSHOT"
componentVersion = "0.3.0.8-SNAPSHOT"
}
repositories {
mavenCentral()
......
package com.ydl.media.audio.manager
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import com.ydl.media.audio.AudioPlayer
import com.ydl.media.audio.PlayService
import com.ydl.media.audio.model.Music
import com.ydl.media.audio.utils.CoverImageUtils
......@@ -17,7 +17,7 @@ import com.ydl.media.audio.utils.CoverImageUtils
*/
class MediaSessionManager private constructor() {
private var playService: PlayService? = null
private var mContext: Context? = null
private var mediaSession: MediaSessionCompat? = null
private val callback = object : MediaSessionCompat.Callback() {
......@@ -50,22 +50,22 @@ class MediaSessionManager private constructor() {
val instance = MediaSessionManager()
}
fun init(playService: PlayService) {
this.playService = playService
fun init(context: Context) {
this.mContext = context
setupMediaSession()
}
private fun setupMediaSession() {
mediaSession = MediaSessionCompat(playService!!, TAG)
mediaSession!!.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS or MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS)
mediaSession!!.setCallback(callback)
mediaSession!!.isActive = true
mediaSession = MediaSessionCompat(mContext, TAG)
mediaSession?.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS or MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS)
mediaSession?.setCallback(callback)
mediaSession?.isActive = true
}
fun updatePlaybackState() {
val state =
if (AudioPlayer.get().isPlaying || AudioPlayer.get().isPreparing) PlaybackStateCompat.STATE_PLAYING else PlaybackStateCompat.STATE_PAUSED
mediaSession!!.setPlaybackState(
mediaSession?.setPlaybackState(
PlaybackStateCompat.Builder()
.setActions(MEDIA_SESSION_ACTIONS)
.setState(state, AudioPlayer.get().audioPosition, 1f)
......@@ -75,7 +75,7 @@ class MediaSessionManager private constructor() {
fun updateMetaData(music: Music?) {
if (music == null) {
mediaSession!!.setMetadata(null)
mediaSession?.setMetadata(null)
return
}
......@@ -95,7 +95,7 @@ class MediaSessionManager private constructor() {
// TODO: 2019-10-26 by:HaoRui
// metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, AppCache.get().getLocalMusicList().size());
}
mediaSession!!.setMetadata(metaData.build())
mediaSession?.setMetadata(metaData.build())
}
})
}
......
......@@ -18,7 +18,6 @@ import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.NotificationTarget
import com.ydl.media.R
import com.ydl.media.audio.PlayService
import com.ydl.media.audio.constants.Extras
import com.ydl.media.audio.model.Music
import com.ydl.media.audio.receiver.StatusBarReceiver
......@@ -30,32 +29,31 @@ import java.util.*
* Des: 通知管理器
*/
class NotifyManager private constructor() {
private var playService: PlayService? = null
private var mContext: Context? = null
private var notificationManager: NotificationManager? = null
private object SingletonHolder {
val instance = NotifyManager()
}
fun init(playService: PlayService) {
this.playService = playService
fun init(context: Context) {
this.mContext = context
notificationManager =
playService.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mContext?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
fun showPlay(music: Music?) {
if (music == null) {
return
}
playService?.startForeground(NOTIFICATION_ID, buildNotification(playService!!, music, true))
}
fun showPause(music: Music?) {
if (music == null) {
return
}
playService?.stopForeground(false)
notificationManager?.notify(NOTIFICATION_ID, buildNotification(playService!!, music, false))
notificationManager?.notify(NOTIFICATION_ID,
mContext?.let { buildNotification(it, music, false) })
}
fun cancelAll() {
......@@ -119,7 +117,7 @@ class NotifyManager private constructor() {
remoteViews.setTextViewText(R.id.tv_title, title)
remoteViews.setTextViewText(R.id.tv_subtitle, subtitle)
val isLightNotificationTheme = isLightNotificationTheme(playService)
val isLightNotificationTheme = isLightNotificationTheme(mContext)
val playIntent = Intent(StatusBarReceiver.ACTION_STATUS_BAR)
playIntent.setPackage("com.cxzapp.xinlizixun")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment