@@ -5,6 +5,9 @@ import android.app.NotificationManager
55import android.app.PendingIntent
66import android.app.Service
77import android.content.Intent
8+ import android.media.AudioAttributes
9+ import android.media.MediaPlayer
10+ import android.net.Uri
811import android.os.Build
912import android.os.IBinder
1013import androidx.core.app.NotificationCompat
@@ -13,6 +16,7 @@ import android.support.v4.media.session.MediaSessionCompat
1316class MusicService : Service () {
1417
1518 private lateinit var mediaSession: MediaSessionCompat
19+ private var mediaPlayer: MediaPlayer ? = null
1620
1721 override fun onCreate () {
1822 super .onCreate()
@@ -24,33 +28,67 @@ class MusicService : Service() {
2428 val action = intent?.action
2529 val title = intent?.getStringExtra(" songTitle" ) ? : " Dada Music Player"
2630 val isPlaying = intent?.getBooleanExtra(" isPlaying" , true ) ? : true
31+ val songUriString = intent?.getStringExtra(" songUri" )
2732
28- if (action == " STOP" ) {
29- stopForeground(STOP_FOREGROUND_REMOVE )
30- stopSelf()
31- return START_NOT_STICKY
33+ when (action) {
34+ " PLAY_URI" -> {
35+ songUriString?.let { uriStr ->
36+ playAudioUri(Uri .parse(uriStr))
37+ }
38+ }
39+ " PAUSE" -> {
40+ mediaPlayer?.pause()
41+ }
42+ " RESUME" -> {
43+ mediaPlayer?.start()
44+ }
45+ " STOP" -> {
46+ mediaPlayer?.stop()
47+ mediaPlayer?.release()
48+ mediaPlayer = null
49+ stopForeground(STOP_FOREGROUND_REMOVE )
50+ stopSelf()
51+ return START_NOT_STICKY
52+ }
3253 }
3354
34- showNotification(title, isPlaying)
55+ showNotification(title, mediaPlayer?. isPlaying ? : false )
3556 return START_STICKY
3657 }
3758
59+ private fun playAudioUri (uri : Uri ) {
60+ try {
61+ mediaPlayer?.stop()
62+ mediaPlayer?.release()
63+ mediaPlayer = MediaPlayer ().apply {
64+ setAudioAttributes(
65+ AudioAttributes .Builder ()
66+ .setContentType(AudioAttributes .CONTENT_TYPE_MUSIC )
67+ .setUsage(AudioAttributes .USAGE_MEDIA )
68+ .build()
69+ )
70+ setDataSource(applicationContext, uri)
71+ prepare()
72+ start()
73+ }
74+ } catch (e: Exception ) {
75+ e.printStackTrace()
76+ }
77+ }
78+
3879 private fun showNotification (title : String , isPlaying : Boolean ) {
3980 val intent = Intent (this , MainActivity ::class .java)
4081 val pendingIntent = PendingIntent .getActivity(
4182 this , 0 , intent,
4283 PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
4384 )
4485
45- val playPauseIcon = if (isPlaying) android.R .drawable.ic_media_pause else android.R .drawable.ic_media_play
46-
4786 val notification = NotificationCompat .Builder (this , " CHANNEL_ID" )
4887 .setContentTitle(title)
49- .setContentText(" Playing in background " )
88+ .setContentText(" Dada Music Playing... " )
5089 .setSmallIcon(android.R .drawable.ic_media_play)
5190 .setContentIntent(pendingIntent)
5291 .setOngoing(isPlaying)
53- .addAction(playPauseIcon, if (isPlaying) " Pause" else " Play" , pendingIntent)
5492 .setStyle(
5593 androidx.media.app.NotificationCompat .MediaStyle ()
5694 .setMediaSession(mediaSession.sessionToken)
@@ -74,5 +112,11 @@ class MusicService : Service() {
74112 }
75113 }
76114
115+ override fun onDestroy () {
116+ super .onDestroy()
117+ mediaPlayer?.release()
118+ mediaPlayer = null
119+ }
120+
77121 override fun onBind (intent : Intent ? ): IBinder ? = null
78122}
0 commit comments