|
|
@ -9,6 +9,8 @@ import android.os.Build |
|
|
import android.os.IBinder |
|
|
import android.os.IBinder |
|
|
import android.util.Log |
|
|
import android.util.Log |
|
|
import androidx.core.app.NotificationCompat |
|
|
import androidx.core.app.NotificationCompat |
|
|
|
|
|
import android.support.v4.media.session.MediaSessionCompat |
|
|
|
|
|
import android.support.v4.media.session.PlaybackStateCompat |
|
|
import com.yunqiinnovation.deepvoice_utils.FileLogger |
|
|
import com.yunqiinnovation.deepvoice_utils.FileLogger |
|
|
import java.util.concurrent.atomic.AtomicBoolean |
|
|
import java.util.concurrent.atomic.AtomicBoolean |
|
|
|
|
|
|
|
|
@ -28,7 +30,7 @@ class MusicService : Service() { |
|
|
fun isPlaying(): Boolean = currentStatus |
|
|
fun isPlaying(): Boolean = currentStatus |
|
|
fun getCurrentUrl(): String? = currentUrlInternal |
|
|
fun getCurrentUrl(): String? = currentUrlInternal |
|
|
} |
|
|
} |
|
|
|
|
|
private lateinit var mediaSession: MediaSessionCompat |
|
|
private var mediaPlayer: MediaPlayer? = null |
|
|
private var mediaPlayer: MediaPlayer? = null |
|
|
private val playlist = mutableListOf<String>() // 可选:初始化为预置歌曲 |
|
|
private val playlist = mutableListOf<String>() // 可选:初始化为预置歌曲 |
|
|
private var currentIndex = 0 |
|
|
private var currentIndex = 0 |
|
|
@ -38,9 +40,38 @@ class MusicService : Service() { |
|
|
FileLogger.init(this) |
|
|
FileLogger.init(this) |
|
|
FileLogger.i(TAG, "MusicService 正在创建...") |
|
|
FileLogger.i(TAG, "MusicService 正在创建...") |
|
|
isRunning.set(true) |
|
|
isRunning.set(true) |
|
|
Log.i(TAG, "MusicService Created") |
|
|
mediaSession = MediaSessionCompat(this, TAG).apply { |
|
|
|
|
|
setCallback(object : MediaSessionCompat.Callback() { |
|
|
|
|
|
override fun onPlay() { |
|
|
|
|
|
resume() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onPause() { |
|
|
|
|
|
pause() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onSkipToNext() { |
|
|
|
|
|
if (currentIndex + 1 < playlist.size) { |
|
|
|
|
|
currentIndex++ |
|
|
|
|
|
play(playlist[currentIndex]) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onSkipToPrevious() { |
|
|
|
|
|
if (currentIndex - 1 >= 0) { |
|
|
|
|
|
currentIndex-- |
|
|
|
|
|
play(playlist[currentIndex]) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onStop() { |
|
|
|
|
|
stopSelf() |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
isActive = true |
|
|
|
|
|
} |
|
|
startForeground(NOTIFICATION_ID, createNotification("等待播放...")) |
|
|
startForeground(NOTIFICATION_ID, createNotification("等待播放...")) |
|
|
isRunning.set(true) |
|
|
Log.i(TAG, "MusicService Created") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { |
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { |
|
|
@ -173,15 +204,14 @@ class MusicService : Service() { |
|
|
manager.createNotificationChannel(channel) |
|
|
manager.createNotificationChannel(channel) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
val intent = packageManager.getLaunchIntentForPackage(packageName)?.apply { |
|
|
// 修复 pendingIntent:打开 App 主 Activity |
|
|
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP |
|
|
val activityIntent = packageManager.getLaunchIntentForPackage(packageName) |
|
|
} |
|
|
val pendingIntent = PendingIntent.getActivity( |
|
|
val pendingIntent = intent?.let { |
|
|
this, |
|
|
PendingIntent.getActivity( |
|
|
0, |
|
|
this, 0, it, |
|
|
activityIntent, |
|
|
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT |
|
|
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT |
|
|
) |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return NotificationCompat.Builder(this, CHANNEL_ID) |
|
|
return NotificationCompat.Builder(this, CHANNEL_ID) |
|
|
.setContentTitle("DeepSound") |
|
|
.setContentTitle("DeepSound") |
|
|
@ -189,6 +219,39 @@ class MusicService : Service() { |
|
|
.setSmallIcon(android.R.drawable.ic_media_play) |
|
|
.setSmallIcon(android.R.drawable.ic_media_play) |
|
|
.setContentIntent(pendingIntent) |
|
|
.setContentIntent(pendingIntent) |
|
|
.setOngoing(true) |
|
|
.setOngoing(true) |
|
|
|
|
|
.setStyle( |
|
|
|
|
|
androidx.media.app.NotificationCompat.MediaStyle() |
|
|
|
|
|
.setMediaSession(mediaSession.sessionToken) |
|
|
|
|
|
.setShowActionsInCompactView(0, 1, 2) |
|
|
|
|
|
) |
|
|
|
|
|
.addAction( |
|
|
|
|
|
NotificationCompat.Action( |
|
|
|
|
|
android.R.drawable.ic_media_previous, "上一曲", |
|
|
|
|
|
androidx.media.session.MediaButtonReceiver.buildMediaButtonPendingIntent( |
|
|
|
|
|
this, |
|
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
.addAction( |
|
|
|
|
|
NotificationCompat.Action( |
|
|
|
|
|
if (currentStatus) android.R.drawable.ic_media_pause else android.R.drawable.ic_media_play, |
|
|
|
|
|
if (currentStatus) "暂停" else "播放", |
|
|
|
|
|
androidx.media.session.MediaButtonReceiver.buildMediaButtonPendingIntent( |
|
|
|
|
|
this, |
|
|
|
|
|
PlaybackStateCompat.ACTION_PLAY_PAUSE |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
.addAction( |
|
|
|
|
|
NotificationCompat.Action( |
|
|
|
|
|
android.R.drawable.ic_media_next, "下一曲", |
|
|
|
|
|
androidx.media.session.MediaButtonReceiver.buildMediaButtonPendingIntent( |
|
|
|
|
|
this, |
|
|
|
|
|
PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
.build() |
|
|
.build() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|