Skip to content
This repository has been archived by the owner on Aug 23, 2024. It is now read-only.

Commit

Permalink
build 293
Browse files Browse the repository at this point in the history
  • Loading branch information
Moriafly committed Jan 26, 2021
1 parent d17c10a commit a6a287c
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 158 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {
applicationId "com.dirror.music"
minSdkVersion 23
targetSdkVersion 30
versionCode 292
versionCode 293
versionName "2.4.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface MusicBinderInterface {

/**
* 改变播放状态
* 取消过时,继续用,也可以用 [start] 或者 [pause]
* 取消过时,继续用,也可以用 [play] 或者 [pause]
*/
fun changePlayState()

Expand Down Expand Up @@ -68,7 +68,7 @@ interface MusicBinderInterface {
/**
* 播放上一首
*/
fun playLast()
fun playPrevious()

/**
* 播放下一首
Expand Down Expand Up @@ -118,7 +118,7 @@ interface MusicBinderInterface {
/**
* 开始播放
*/
fun start()
fun play()

/**
* 暂停播放
Expand Down
36 changes: 18 additions & 18 deletions app/src/main/java/com/dirror/music/service/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.jetbrains.annotations.TestOnly
* 音乐服务
* @author Moriafly
* @since 2020/9
* TODO 蓝牙耳机切歌、暂停
*/
class MusicService : Service() {

Expand Down Expand Up @@ -111,9 +112,9 @@ class MusicService : Service() {
.setAudioAttributes(audioAttributes)
.setOnAudioFocusChangeListener { focusChange ->
when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN -> musicBinder.start()
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT -> musicBinder.start()
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK -> musicBinder.start()
AudioManager.AUDIOFOCUS_GAIN -> musicBinder.play()
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT -> musicBinder.play()
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK -> musicBinder.play()
AudioManager.AUDIOFOCUS_LOSS -> musicBinder.pause()
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> musicBinder.pause()
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> musicBinder.pause()
Expand All @@ -127,8 +128,7 @@ class MusicService : Service() {
}

/**
* 初始化 MediaSession 回调
* 媒体绘话的回调
* 初始化媒体会话 MediaSession
*/
private fun initMediaSession() {
val intentFilter = IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)
Expand Down Expand Up @@ -214,15 +214,21 @@ class MusicService : Service() {
when (keyEvent.keyCode) {
KeyEvent.KEYCODE_MEDIA_PLAY -> { // 播放按钮
// toast("KEY_PLAY")
MyApplication.musicBinderInterface?.play()
}
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> {
MyApplication.musicBinderInterface?.changePlayState()
}
KeyEvent.KEYCODE_MEDIA_PAUSE -> {
MyApplication.musicBinderInterface?.pause()
}
KeyEvent.KEYCODE_MEDIA_NEXT -> { // 下一首
// toast("KEY_NEXT")
MyApplication.musicBinderInterface?.playNext()
}
KeyEvent.KEYCODE_MEDIA_PREVIOUS -> { // 上一首
// toast("KEY_PREVIOUS")
MyApplication.musicBinderInterface?.playLast()
MyApplication.musicBinderInterface?.playPrevious()
}
}
}
Expand All @@ -237,11 +243,6 @@ class MusicService : Service() {
}
// 初始化 MediaSession
mediaSession = MediaSessionCompat(this, "MusicService").apply {
// 监听按键
setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
)
// 设置 Callback
setCallback(mediaSessionCallback, Handler(Looper.getMainLooper()))
// 把 MediaSession 置为 active,这样才能开始接收各种信息
Expand All @@ -253,12 +254,12 @@ class MusicService : Service() {

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.getIntExtra("int_code", 0)) {
CODE_PREVIOUS -> musicBinder.playLast()
CODE_PREVIOUS -> musicBinder.playPrevious()
CODE_PLAY -> {
if (musicBinder.getPlayState()) {
musicBinder.pause()
} else {
musicBinder.start()
musicBinder.play()
}
}
CODE_NEXT -> musicBinder.playNext()
Expand Down Expand Up @@ -442,7 +443,7 @@ class MusicService : Service() {
/**
* 开始播放
*/
override fun start() {
override fun play() {
mediaPlayer?.start()
mediaSessionCallback?.onPlay()
sendMusicBroadcast()
Expand Down Expand Up @@ -529,7 +530,7 @@ class MusicService : Service() {
/**
* 播放上一曲
*/
override fun playLast() {
override fun playPrevious() {
// 设置 position
position = when (mode) {
MODE_RANDOM -> {
Expand Down Expand Up @@ -693,7 +694,7 @@ class MusicService : Service() {
// 单曲循环
MODE_REPEAT_ONE -> {
setProgress(0)
start()
play()
return
}
MODE_RANDOM -> {
Expand Down Expand Up @@ -825,5 +826,4 @@ class MusicService : Service() {
R.drawable.ic_baseline_play_arrow_24
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,93 +1,95 @@
//package com.dirror.music.service
//
//import android.os.Bundle
//import android.support.v4.media.MediaBrowserCompat
//import android.support.v4.media.session.MediaSessionCompat
//import android.support.v4.media.session.PlaybackStateCompat
//import androidx.media.MediaBrowserServiceCompat
//import com.umeng.commonsdk.statistics.AnalyticsConstants.LOG_TAG
//
///**
// * MediaPlaybackService
// * @author Moriafly
// * @since 2021年1月25日19:19:58
// */
//private const val MY_MEDIA_ROOT_ID = "media_root_id"
//private const val MY_EMPTY_MEDIA_ROOT_ID = "empty_root_id"
//
//class MediaPlaybackService : MediaBrowserServiceCompat() {
//
// private var mediaSession: MediaSessionCompat? = null
// private lateinit var stateBuilder: PlaybackStateCompat.Builder
//
// override fun onCreate() {
// super.onCreate()
//
// // Create a MediaSessionCompat
// mediaSession = MediaSessionCompat(baseContext, LOG_TAG).apply {
//
// // Enable callbacks from MediaButtons and TransportControls
package com.dirror.music.service.test

import android.os.Bundle
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.media.MediaBrowserServiceCompat

/**
* MediaPlaybackService
* @author Moriafly
* @since 2021年1月25日19:19:58
*/
private const val MY_MEDIA_ROOT_ID = "media_root_id"
private const val MY_EMPTY_MEDIA_ROOT_ID = "empty_root_id"

class MediaPlaybackService : MediaBrowserServiceCompat() {

private var mediaSession: MediaSessionCompat? = null
private lateinit var stateBuilder: PlaybackStateCompat.Builder

override fun onCreate() {
super.onCreate()

// Create a MediaSessionCompat
mediaSession = MediaSessionCompat(baseContext, "DsoMusicServiceTag").apply {

// Enable callbacks from MediaButtons and TransportControls
// setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
// or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
// )
//
// // Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
// stateBuilder = PlaybackStateCompat.Builder()
// .setActions(PlaybackStateCompat.ACTION_PLAY
// or PlaybackStateCompat.ACTION_PLAY_PAUSE
// )
// setPlaybackState(stateBuilder.build())
//
// // MySessionCallback() has methods that handle callbacks from a media controller
// setCallback(MySessionCallback())
//
// // Set the session's token so that client activities can communicate with it.
// setSessionToken(sessionToken)
// }
// }
//
// override fun onGetRoot(
// clientPackageName: String,
// clientUid: Int,
// rootHints: Bundle?
// ): MediaBrowserServiceCompat.BrowserRoot {
//
// // (Optional) Control the level of access for the specified package name.
// // You'll need to write your own logic to do this.

// Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
stateBuilder = PlaybackStateCompat.Builder()
.setActions(PlaybackStateCompat.ACTION_PLAY
or PlaybackStateCompat.ACTION_PLAY_PAUSE
)
setPlaybackState(stateBuilder.build())

// MySessionCallback() has methods that handle callbacks from a media controller
setCallback(MediaSessionCallback())

// Set the session's token so that client activities can communicate with it.
setSessionToken(sessionToken)
}
}

override fun onGetRoot(
clientPackageName: String,
clientUid: Int,
rootHints: Bundle?
): BrowserRoot {

// (Optional) Control the level of access for the specified package name.
// You'll need to write your own logic to do this.
// return if (allowBrowsing(clientPackageName, clientUid)) {
// // Returns a root ID that clients can use with onLoadChildren() to retrieve
// // the content hierarchy.
// MediaBrowserServiceCompat.BrowserRoot(MY_MEDIA_ROOT_ID, null)
// BrowserRoot(MY_MEDIA_ROOT_ID, null)
// } else {
// // Clients can connect, but this BrowserRoot is an empty hierachy
// // so onLoadChildren returns nothing. This disables the ability to browse for content.
// MediaBrowserServiceCompat.BrowserRoot(MY_EMPTY_MEDIA_ROOT_ID, null)
// }
// }
//
// override fun onLoadChildren(
// parentMediaId: String,
// result: MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>>
// ) {
// // Browsing not allowed
// if (MY_EMPTY_MEDIA_ROOT_ID == parentMediaId) {
// result.sendResult(null)
// return
// }
//
// // Assume for example that the music catalog is already loaded/cached.
//
// val mediaItems = emptyList<MediaBrowserCompat.MediaItem>()
//
// // Check if this is the root menu:
// if (MY_MEDIA_ROOT_ID == parentMediaId) {
// // Build the MediaItem objects for the top level,
// // and put them in the mediaItems list...
// } else {
// // Examine the passed parentMediaId to see which submenu we're at,
// // and put the children of that menu in the mediaItems list...
// BrowserRoot(MY_EMPTY_MEDIA_ROOT_ID, null)
// }
// result.sendResult(mediaItems)
// }
//
//}
return BrowserRoot(MY_EMPTY_MEDIA_ROOT_ID, null)
}

override fun onLoadChildren(
parentMediaId: String,
result: Result<List<MediaBrowserCompat.MediaItem>>
) {
// Browsing not allowed
if (MY_EMPTY_MEDIA_ROOT_ID == parentMediaId) {
result.sendResult(null)
return
}

// Assume for example that the music catalog is already loaded/cached.

val mediaItems = emptyList<MediaBrowserCompat.MediaItem>()

// Check if this is the root menu:
if (MY_MEDIA_ROOT_ID == parentMediaId) {
// Build the MediaItem objects for the top level,
// and put them in the mediaItems list...
} else {
// Examine the passed parentMediaId to see which submenu we're at,
// and put the children of that menu in the mediaItems list...
}
result.sendResult(mediaItems)
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dirror.music.service.test

import android.support.v4.media.session.MediaSessionCompat

class MediaSessionCallback : MediaSessionCompat.Callback() {

}
Loading

0 comments on commit a6a287c

Please sign in to comment.