Skip to content

Commit

Permalink
1.播放器循环模式增加注解限定值范围,避免参数错误
Browse files Browse the repository at this point in the history
2.进度监听增加标签,开放 removeProgressListener
3.targetSdkVersion升级到31
4.通过WorkManager来启动安卓12的后台服务(比如刷新通知栏等行为)
5.播放过程中添加唤醒锁和释放锁,避免部分机型后台播放过程中网络断开
  • Loading branch information
lizixian committed Jan 17, 2023
1 parent a53d66b commit 1811d0d
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 64 deletions.
7 changes: 7 additions & 0 deletions app/src/main/assets/home.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{
albummid: "003g4JnU23vCjt",
albumname: "2005 年终舞曲总冠军",
songUrl: "https://ws.stream.qqmusic.qq.com/C400000o6bhv2ugwjN.m4a?guid=69114368&vkey=B30D13A2E97D318F346389E146E646C02AE6EEF6FCE399230482F16EB30D6ED4859E3C97F08DF63548EC9342B119BD4A5EBA74B19CF9156B&uin=&fromtag=103032",
singer: [
{
id: 4884,
Expand All @@ -17,6 +18,7 @@
{
albummid: "001IPNxq3vvwa8",
albumname: "Hear My Voice",
songUrl: "https://m10.music.126.net/20230104144944/85fa6e42df2c810fbeb360424b42b1ed/ymusic/a2eb/2b04/1625/56952844ffe4cdb4337b769e0c8f1577.mp3",
singer: [
{
id: 265,
Expand All @@ -30,6 +32,7 @@
{
albummid: "0048pWY92Ex9T4",
albumname: "无尽的爱",
songUrl: "https://ws.stream.qqmusic.qq.com/C400001Ymwjm0egSJE.m4a?guid=5314973&vkey=CC91397263F966B66EC4817CD0AEF7BE329D2ED6CD4B03A0B616F217B964E4CA89E9F5C4A67018636BD7110F389FFCF7A570E9AC91E19C91&uin=626567678&fromtag=103032",
singer: [
{
id: 13,
Expand All @@ -43,6 +46,7 @@
{
albummid: "0024bjiL2aocxT",
albumname: "十一月的萧邦",
songUrl: "https://ws.stream.qqmusic.qq.com/C400000bDWCU1xfx7q.m4a?guid=8723755&vkey=EF24F107D5D5610A6E8AA15CDF2C3C819DF8509ED2A7836BE2D11FC54DE8A020CB43BECC73F54AF0F1E1BE63BAA8A98EA950460F775B75D2&uin=626567678&fromtag=103032",
singer: [
{
id: 4558,
Expand All @@ -56,6 +60,7 @@
{
albummid: "002gMsEJ3gUI0b",
albumname: "琦乐无穷",
songUrl: "https://m10.music.126.net/20230104145129/7e2bb135762caab092755b2e368c3a09/ymusic/obj/w5zDlMODwrDDiGjCn8Ky/3944283695/8307/7c9a/e90b/35eed2d6cda7bd99343f36c654fa16da.mp3",
singer: [
{
id: 4933,
Expand All @@ -69,6 +74,7 @@
{
albummid: "0024bjiL2aocxT",
albumname: "十一月的萧邦",
songUrl: "https://isure.stream.qqmusic.qq.com/M5000046tRJB3SkUUV.mp3?guid=1371522944&vkey=F681D178BDC2ED0C3C4FC5E426E2D0BD4A82B10740FF455EE0C019BA97D0121686361C2E98A708E0CFCAFB446DF28465515A15E0F87F512B&uin=&fromtag=120042",
singer: [
{
id: 4558,
Expand All @@ -82,6 +88,7 @@
{
albummid: "0025CwW50uC4hZ",
albumname: "高手",
"songUrl": "https://isure.stream.qqmusic.qq.com/M500002YaOAQ0aBllE.mp3?guid=1161794620&vkey=0DFA9B45696048D691751F0F20D5FDD852767A1815B34988F9C407268865D0701BD49A258CEC8DD097B23BA60B8C5B947E13FE8E3472197A&uin=&fromtag=120042",
singer: [
{
id: 4359,
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/lzx/musiclib/home/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MainActivity : AppCompatActivity() {
initRecycleView(list)
}

StarrySky.with().playbackState().observe(this, {
StarrySky.with().playbackState().observe(this) {
if (it.songInfo?.tag != "home") return@observe
when (it.stage) {
PlaybackStage.PLAYING -> {
Expand All @@ -72,7 +72,7 @@ class MainActivity : AppCompatActivity() {
}
}
}
})
}
StarrySky.with().setOnPlayProgressListener(object : OnPlayProgressListener {
override fun onPlayProgress(currPos: Long, duration: Long) {
val info = StarrySky.with().getNowPlayingSongInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class MusicViewModel : ViewModel() {
info.artist = it?.getArray("singer")?.getJSONObject(0)?.getString("name").orEmpty()
val albumid = it?.getString("albummid").orEmpty()
info.songCover = "https://y.gtimg.cn/music/photo_new/T002R300x300M000${albumid}.jpg"
info.songUrl = if (it?.has("songUrl") == true) {
it.getString("songUrl").orEmpty()
} else {
""
}
list.add(info)
}
return list
Expand Down
17 changes: 6 additions & 11 deletions starrysky/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ apply plugin: 'kotlin-android-extensions'
//apply plugin: 'com.novoda.bintray-release'

android {
compileSdkVersion 28
compileSdkVersion 31

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
targetSdkVersion 31
versionCode 263
versionName "2.6.3"

Expand All @@ -34,21 +34,16 @@ android {
experimental = true
}

// publish {
// repoName = 'StarrySky'
// userOrg = 'lizixian'
// groupId = 'com.lzx'
// artifactId = 'StarrySkyX'
// publishVersion = '2.6.3'
// desc = 'A Powerful and Streamline MusicLibrary'
// website = "https://github.com/lizixian18/StarrySky"
// }
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.work:work-runtime-ktx:2.7.1'
api "com.google.android.exoplayer:exoplayer-core:2.14.1"
api "com.google.android.exoplayer:extension-mediasession:2.14.1"
compileOnly ("com.google.android.exoplayer:exoplayer-dash:2.14.1")
Expand Down
1 change: 1 addition & 0 deletions starrysky/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application android:networkSecurityConfig="@xml/network_security_config">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ class AppLifecycleCallback : ActivityLifecycleCallbacks {

fun getStackTopActivity() = activityStack.getOrNull(activityStack.lastIndex)

override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
activityStack.add(activity)
}

override fun onActivityStarted(activity: Activity?) {
override fun onActivityStarted(activity: Activity) {
}

override fun onActivityResumed(activity: Activity?) {
override fun onActivityResumed(activity: Activity) {
currActivity = activity
}

override fun onActivityPaused(activity: Activity?) {
override fun onActivityPaused(activity: Activity) {
currActivity = null
}

override fun onActivityStopped(activity: Activity?) {
override fun onActivityStopped(activity: Activity) {
}

override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
}

override fun onActivityDestroyed(activity: Activity?) {
override fun onActivityDestroyed(activity: Activity) {
activityStack.remove(activity)
StarrySky.with().removeProgressListener(activity)
StarrySky.with().removeProgressListener(activity.toString())
StarrySky.with().resetVariable(activity)
}
}
2 changes: 1 addition & 1 deletion starrysky/src/main/java/com/lzx/starrysky/StarrySky.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object StarrySky {
StarrySkyInstall.interceptors.clear()
}

internal fun log(msg: String?) {
internal fun log(msg: String) {
if (StarrySkyInstall.isDebug) {
Log.i("StarrySky", msg)
}
Expand Down
4 changes: 2 additions & 2 deletions starrysky/src/main/java/com/lzx/starrysky/StarrySkyPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ class StarrySkyPlayer(private var userGlobalConfig: Boolean = true) {
return playerControl!!
}

fun release(activity: Activity?) {
with().removeProgressListener(activity)
fun release(activity: Activity) {
with().removeProgressListener(activity.toString())
with().resetVariable(activity)
}
}
26 changes: 14 additions & 12 deletions starrysky/src/main/java/com/lzx/starrysky/control/PlayerControl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ class PlayerControl(
}
}

// internal fun attachPlayerCallback() {
// playbackManager.attachPlayerCallback(this)
// }

/**
* 是否跳过播放队列,false的话,播放将不经过播放队列,直接走播放器,当前Activity结束后恢复false状态
*/
Expand Down Expand Up @@ -274,7 +270,7 @@ class PlayerControl(
*
* isLoop 播放倒最后一首时是否从第一首开始循环播放,该参数对随机播放无效
*/
fun setRepeatMode(repeatMode: Int, isLoop: Boolean) {
fun setRepeatMode(@RepeatModeFlag repeatMode: Int, isLoop: Boolean) {
if (isSkipMediaQueue && repeatMode != RepeatMode.REPEAT_MODE_ONE) {
throw IllegalStateException("isSkipMediaQueue 模式下只能设置单曲模式")
}
Expand Down Expand Up @@ -567,21 +563,26 @@ class PlayerControl(

/**
* 设置进度监听
* tag:进度标记
*/
fun setOnPlayProgressListener(listener: OnPlayProgressListener) {
val pkgActivityName = StarrySky.getStackTopActivity()?.toString()
pkgActivityName?.let {
fun setOnPlayProgressListener(
listener: OnPlayProgressListener,
tag: String? = StarrySky.getStackTopActivity()?.toString()
) {
tag?.let {
progressListener.put(it, listener)
}
if (!isRunningTimeTask && isPlaying()) {
timerTaskManager?.startToUpdateProgress()
}
}

internal fun removeProgressListener(activity: Activity?) {
activity?.let {
progressListener.remove(it.toString())
}
/**
* 移除进度监听
* tag:进度标记
*/
fun removeProgressListener(tag: String) {
progressListener.remove(tag)
}

fun onPlaybackStateUpdated(playbackStage: PlaybackStage) {
Expand Down Expand Up @@ -613,6 +614,7 @@ class PlayerControl(
}

fun resetVariable(activity: Activity?) {
interceptors.clear()
playbackManager.resetVariable(activity)
}

Expand Down
10 changes: 10 additions & 0 deletions starrysky/src/main/java/com/lzx/starrysky/control/RepeatMode.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lzx.starrysky.control

import androidx.annotation.IntDef
import com.lzx.starrysky.utils.StarrySkyConstant
import org.json.JSONObject

Expand Down Expand Up @@ -40,6 +41,15 @@ data class RepeatMode(val repeatMode: Int, val isLoop: Boolean) {
}
}

@IntDef(
RepeatMode.REPEAT_MODE_NONE,
RepeatMode.REPEAT_MODE_ONE,
RepeatMode.REPEAT_MODE_SHUFFLE,
RepeatMode.REPEAT_MODE_REVERSE
)
@Retention(AnnotationRetention.SOURCE)
annotation class RepeatModeFlag

fun Int.isModeNone() = this == RepeatMode.REPEAT_MODE_NONE
fun Int.isModeOne() = this == RepeatMode.REPEAT_MODE_ONE
fun Int.isModeShuffle() = this == RepeatMode.REPEAT_MODE_SHUFFLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ class CustomNotification constructor(
filter.addAction(ACTION_PLAY_OR_PAUSE)
filter.addAction(ACTION_CLOSE)
context.registerReceiver(this, filter)
(context as MusicService).startForeground(NOTIFICATION_ID, notification)
(context as MusicService).customStartForeground(NOTIFICATION_ID, notification)
mStarted = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class SystemNotification constructor(
filter.addAction(ACTION_PLAY)
filter.addAction(ACTION_PREV)
context.registerReceiver(this, filter)
(context as MusicService).startForeground(INotification.NOTIFICATION_ID, notification)
(context as MusicService).customStartForeground(INotification.NOTIFICATION_ID, notification)
mStarted = true
}
}
Expand Down
Loading

0 comments on commit 1811d0d

Please sign in to comment.