diff --git a/app/src/main/java/com/ztftrue/music/play/CreateNotification.kt b/app/src/main/java/com/ztftrue/music/play/CreateNotification.kt index f93e6cc..1ab67f6 100644 --- a/app/src/main/java/com/ztftrue/music/play/CreateNotification.kt +++ b/app/src/main/java/com/ztftrue/music/play/CreateNotification.kt @@ -1,6 +1,5 @@ package com.ztftrue.music.play -import android.Manifest import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent @@ -8,13 +7,10 @@ import android.app.Service import android.app.Service.STOP_FOREGROUND_REMOVE import android.content.Context import android.content.Intent -import android.content.pm.PackageManager import android.support.v4.media.MediaMetadataCompat import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat -import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat -import androidx.core.app.NotificationManagerCompat import androidx.media.session.MediaButtonReceiver import androidx.media3.common.util.UnstableApi import com.ztftrue.music.MainActivity @@ -42,24 +38,24 @@ class CreateNotification(service: Service, private val mediaSession: MediaSessio .setContentIntent(pendingContentIntent) .setPriority(NotificationCompat.PRIORITY_DEFAULT) service.startForeground(NOTIFICATION_ID, builder.build()) - if (ActivityCompat.checkSelfPermission( - service, - Manifest.permission.POST_NOTIFICATIONS - ) == PackageManager.PERMISSION_GRANTED - ) { - with(NotificationManagerCompat.from(service)) { - notify(NOTIFICATION_ID, builder.build()) - } - } +// if (ActivityCompat.checkSelfPermission( +// service, +// Manifest.permission.POST_NOTIFICATIONS +// ) == PackageManager.PERMISSION_GRANTED +// ) { +// with(NotificationManagerCompat.from(service)) { +// notify(NOTIFICATION_ID, builder.build()) +// } +// } } - fun cancelNotification(service: Service) { - with(NotificationManagerCompat.from(service)) { - cancel(NOTIFICATION_ID) - cancelAll() - service.stopForeground(STOP_FOREGROUND_REMOVE) - } + fun cancelNotification() { +// with(NotificationManagerCompat.from(service)) { +// cancel(NOTIFICATION_ID) +// cancelAll() +// service.stopForeground(STOP_FOREGROUND_REMOVE) +// } } fun stop(service: Service) { @@ -100,10 +96,10 @@ class CreateNotification(service: Service, private val mediaSession: MediaSessio ) .build() ) - mediaSession?.setFlags( - MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or - MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS - ) +// mediaSession?.setFlags( +// MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or +// MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS +// ) val builder = NotificationCompat.Builder(service, CHANNEL_ID) .setStyle( androidx.media.app.NotificationCompat.MediaStyle() @@ -173,15 +169,15 @@ class CreateNotification(service: Service, private val mediaSession: MediaSessio // Waring: don't use this it make your app crash,unless you confirm you app need stop // service.stopForeground(STOP_FOREGROUND_DETACH) // } - if (ActivityCompat.checkSelfPermission( - service, - Manifest.permission.POST_NOTIFICATIONS - ) == PackageManager.PERMISSION_GRANTED - ) { - with(NotificationManagerCompat.from(service)) { - notify(NOTIFICATION_ID, builder.build()) - } - } +// if (ActivityCompat.checkSelfPermission( +// service, +// Manifest.permission.POST_NOTIFICATIONS +// ) == PackageManager.PERMISSION_GRANTED +// ) { +// with(NotificationManagerCompat.from(service)) { +// notify(NOTIFICATION_ID, builder.build()) +// } +// } } private fun createNotificationChannel(context: Context) { diff --git a/app/src/main/java/com/ztftrue/music/play/PlayService.kt b/app/src/main/java/com/ztftrue/music/play/PlayService.kt index 2dba6fa..8b37ef7 100644 --- a/app/src/main/java/com/ztftrue/music/play/PlayService.kt +++ b/app/src/main/java/com/ztftrue/music/play/PlayService.kt @@ -890,7 +890,7 @@ class PlayService : MediaBrowserServiceCompat() { // so i add this code, let user exit app. // of course, user can remove notify,make system stop service. exoPlayer.pause() - notify?.cancelNotification(this@PlayService) + notify?.cancelNotification() stopSelf() result.sendResult(null) } else if (ACTION_GET_TRACK_BY_ID == action) { @@ -972,7 +972,7 @@ class PlayService : MediaBrowserServiceCompat() { bundle.putLong("remaining", remainingTime) bundle.putLong("sleepTime", 0L) mediaSession?.setExtras(bundle) - notify?.cancelNotification(this@PlayService) + notify?.cancelNotification() } override fun onGetRoot( @@ -1226,7 +1226,7 @@ class PlayService : MediaBrowserServiceCompat() { stopForeground(STOP_FOREGROUND_REMOVE) stopTimer() saveCurrentDuration(exoPlayer.currentPosition) - notify?.cancelNotification(this@PlayService) + notify?.cancelNotification() mediaSession?.release() // I don't know why sometimes exoPlayer is null, diff --git a/app/src/main/java/com/ztftrue/music/utils/CaptionUtils.kt b/app/src/main/java/com/ztftrue/music/utils/CaptionUtils.kt index 63f729b..83cad4d 100644 --- a/app/src/main/java/com/ztftrue/music/utils/CaptionUtils.kt +++ b/app/src/main/java/com/ztftrue/music/utils/CaptionUtils.kt @@ -1,6 +1,7 @@ package com.ztftrue.music.utils import android.content.Context +import android.util.Log import com.ztftrue.music.R import com.ztftrue.music.utils.model.Caption import com.ztftrue.music.utils.model.ListStringCaption @@ -138,20 +139,25 @@ object CaptionUtils { fun getEmbeddedLyrics(path: String, context: Context): ArrayList { val audioFile = File(path) - val f = AudioFileIO.read(audioFile) - val tag: Tag = f.tag val arrayList = arrayListOf() - val lyrics: String = tag.getFirst(FieldKey.LYRICS) - if (lyrics.trim().isNotEmpty()) { - lyrics.split("\n").forEach { - val captions = parseLyricLine(it, context) - val an = ListStringCaption( - text = ArrayList(captions.text.split(Regex("[\\n\\r\\s]+"))), - timeStart = captions.timeStart, - timeEnd = captions.timeEnd - ) - arrayList.add(an) + + try { + val f = AudioFileIO.read(audioFile) + val tag: Tag = f.tag + val lyrics: String = tag.getFirst(FieldKey.LYRICS) + if (lyrics.trim().isNotEmpty()) { + lyrics.split("\n").forEach { + val captions = parseLyricLine(it, context) + val an = ListStringCaption( + text = ArrayList(captions.text.split(Regex("[\\n\\r\\s]+"))), + timeStart = captions.timeStart, + timeEnd = captions.timeEnd + ) + arrayList.add(an) + } } + } catch (e: Exception) { + Log.e("getEmbeddedLyrics",e.message?:"") } return arrayList }