Skip to content

Commit

Permalink
уведомление появляется вовремя, но не прячется когда нужно
Browse files Browse the repository at this point in the history
NikolayRyabcev committed Apr 24, 2024
1 parent cc7e5be commit abcb1f8
Showing 4 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -7,4 +7,6 @@ interface AudioPlayerControl {
fun getPlayerState(): StateFlow<PlayerState>
fun startPlayer()
fun pausePlayer()
fun provideNotificator()
fun stopNotification()
}
Original file line number Diff line number Diff line change
@@ -53,7 +53,6 @@ class MusicService : Service(), AudioPlayerControl {
override fun onCreate() {
super.onCreate()
mediaPlayer = MediaPlayer()

}

private fun getForegroundServiceTypeConstant(): Int {
@@ -64,6 +63,31 @@ class MusicService : Service(), AudioPlayerControl {
}
}

override fun onBind(intent: Intent?): IBinder? {
songUrl = intent?.getStringExtra("song_url") ?: ""
val artist = intent?.getStringExtra("songArtist") ?: ""
val track = intent?.getStringExtra("songName") ?: ""
trackInfo = "$artist - $track"
Log.d("плеер в onBind", trackInfo)
initMediaPlayer()
return binder
}

//уведомления
override fun provideNotificator() {
createNotificationChannel()
ServiceCompat.startForeground(
this,
SERVICE_NOTIFICATION_ID,
createServiceNotification(),
getForegroundServiceTypeConstant()
)
}

override fun stopNotification() {
stopService(Intent(this, MusicService::class.java))
}

private fun createNotificationChannel() {
Log.d("плеер", "createNotificationChannel")
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
@@ -84,33 +108,15 @@ class MusicService : Service(), AudioPlayerControl {
}

private fun createServiceNotification(): Notification {
Log.d("плеер в createService", trackInfo)
return NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(trackInfo)
.setContentTitle("Playlist Maker")
.setContentText(trackInfo)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build()
}

override fun onBind(intent: Intent?): IBinder? {
songUrl = intent?.getStringExtra("song_url") ?: ""
val artist = intent?.getStringExtra("songArtist") ?: ""
val track = intent?.getStringExtra("songName") ?: ""
trackInfo = "$artist - $track"
Log.d("плеер в onBind", trackInfo)
initMediaPlayer()
createNotificationChannel()
ServiceCompat.startForeground(
this,
SERVICE_NOTIFICATION_ID,
createServiceNotification(),
getForegroundServiceTypeConstant()
)
return binder
}

override fun onUnbind(intent: Intent?): Boolean {
releasePlayer()
return super.onUnbind(intent)
Original file line number Diff line number Diff line change
@@ -189,6 +189,16 @@ class PlayerFragment : Fragment() {
}
}

override fun onPause() {
super.onPause()
playerViewModel.showNotification()
}

override fun onResume() {
super.onResume()
playerViewModel.hideNotification()
}

override fun onStop() {
bottomNavigator.visibility = VISIBLE
super.onStop()
Original file line number Diff line number Diff line change
@@ -118,6 +118,13 @@ class PlayerViewModel(
}
}

fun showNotification (){
audioPlayerControl?.provideNotificator()
}

fun hideNotification (){
audioPlayerControl?.stopNotification()
}
companion object {
const val PLAYER_BUTTON_PRESSING_DELAY = 300L
const val TAG = "PlayerViewModel"

0 comments on commit abcb1f8

Please sign in to comment.