Commit 8cc2f486 by 刘鹏

feat: PlayService解绑

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