diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/application_components/services/music_example/MusicBackgroundService.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/application_components/services/music_example/MusicBackgroundService.kt deleted file mode 100644 index b8f237a4..00000000 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/application_components/services/music_example/MusicBackgroundService.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.example.jetpack_compose_all_in_one.application_components.services.music_example - -class MusicBackgroundService { -} \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/application_components/services/music_example/MusicBoundService.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/application_components/services/music_example/MusicBoundService.kt index 29801ed7..920c4715 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/application_components/services/music_example/MusicBoundService.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/application_components/services/music_example/MusicBoundService.kt @@ -12,47 +12,84 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.launch +/** + * A bound service for managing music playback. + */ class MusicBoundService : Service() { + // Lazy initialization of MediaPlayer instance private val mediaPlayer by lazy { + // Create a MediaPlayer instance with default ringtone URI MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI).apply { + // Set listener to stop service when playback completes setOnCompletionListener { stopSelf() } } } + // Binder instance for allowing interaction with the service private val binder = RemoteControl() + + // Job instance for handling pausing of music private var pauseJob: Job? = null + /** + * Binder class for this service. + */ inner class RemoteControl : Binder() { + // Access to the parent service val service get() = this@MusicBoundService } - override fun onBind(p0: Intent?) = binder + /** + * Called when a client binds to the service. Returns the binder instance. + */ + override fun onBind(intent: Intent?) = binder + /** + * Called when the service is destroyed. Releases MediaPlayer resources. + */ override fun onDestroy() { super.onDestroy() mediaPlayer.release() } + /** + * Starts playing music from the provided URI. + * @param musicUri The URI of the music to be played. + */ fun startMusic(musicUri: Uri) { mediaPlayer.apply { + // Reset the MediaPlayer instance reset() + // Set data source to the provided URI setDataSource(this@MusicBoundService, musicUri) + // Prepare the MediaPlayer asynchronously prepare() + // Start playback start() } } + /** + * Stops the currently playing music. + */ fun stopMusic() { mediaPlayer.stop() } + /** + * Pauses the currently playing music, optionally for a specified duration. + * @param duration The duration for which to pause the music, in milliseconds. + */ fun pauseMusic(duration: Long? = null) { + // Cancel any previous pause job pauseJob?.cancel() pauseJob = null + // Start a new coroutine for pausing the music pauseJob = CoroutineScope(Dispatchers.Default + Job()).launch { mediaPlayer.pause() + // If duration is specified, delay for that duration and then resume music duration?.let { delay(it) resumeMusic() @@ -60,9 +97,13 @@ class MusicBoundService : Service() { } } + /** + * Resumes playback of paused music. + */ fun resumeMusic() { mediaPlayer.start() + // Cancel any ongoing pause job pauseJob?.cancel() pauseJob = null } -} \ No newline at end of file +}