Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: github action error, embedded lyrics crush #19

Merged
merged 3 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 28 additions & 32 deletions app/src/main/java/com/ztftrue/music/play/CreateNotification.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package com.ztftrue.music.play

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/ztftrue/music/play/PlayService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 18 additions & 12 deletions app/src/main/java/com/ztftrue/music/utils/CaptionUtils.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -138,20 +139,25 @@ object CaptionUtils {

fun getEmbeddedLyrics(path: String, context: Context): ArrayList<ListStringCaption> {
val audioFile = File(path)
val f = AudioFileIO.read(audioFile)
val tag: Tag = f.tag
val arrayList = arrayListOf<ListStringCaption>()
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
}
Expand Down
Loading