From 8e40cb73da11f558ae5e68ff18cde425c463f522 Mon Sep 17 00:00:00 2001 From: tuancoltech Date: Mon, 5 Aug 2024 22:25:56 +0700 Subject: [PATCH] Stop Audio playback when view is no longer visible --- .../arkmemo/media/ArkMediaPlayerImpl.kt | 4 ++-- .../dev/arkbuilders/arkmemo/models/VoiceNote.kt | 3 ++- .../arkmemo/ui/adapters/NotesListAdapter.kt | 10 ++++++++++ .../arkmemo/ui/fragments/ArkRecorderFragment.kt | 7 +++++++ .../arkmemo/ui/fragments/NotesFragment.kt | 16 ++++++++++++++++ .../ui/viewmodels/ArkMediaPlayerViewModel.kt | 4 ++++ 6 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/media/ArkMediaPlayerImpl.kt b/app/src/main/java/dev/arkbuilders/arkmemo/media/ArkMediaPlayerImpl.kt index 824e6b96..88ea3477 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/media/ArkMediaPlayerImpl.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/media/ArkMediaPlayerImpl.kt @@ -56,9 +56,9 @@ class ArkMediaPlayerImpl @Inject constructor(): ArkMediaPlayer { override fun duration(): Int = player?.duration ?: 0 - override fun currentPosition(): Int = player?.currentPosition!! + override fun currentPosition(): Int = player?.currentPosition ?: 0 - override fun isPlaying(): Boolean = player?.isPlaying!! + override fun isPlaying(): Boolean = player?.isPlaying ?: false override fun onCompletion(player: MediaPlayer?) { onCompletionHandler() diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/models/VoiceNote.kt b/app/src/main/java/dev/arkbuilders/arkmemo/models/VoiceNote.kt index 9f9f1429..83029dfb 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/models/VoiceNote.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/models/VoiceNote.kt @@ -16,5 +16,6 @@ class VoiceNote( var path: Path = createTempFile(), @IgnoredOnParcel override var resource: Resource? = null, - override var pendingForDelete: Boolean = false + override var pendingForDelete: Boolean = false, + var isPlaying: Boolean = false ): Note, Parcelable \ No newline at end of file diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt index 09640229..504ef131 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/adapters/NotesListAdapter.kt @@ -75,9 +75,19 @@ class NotesListAdapter( holder.btnPlayPause.setOnClickListener { onPlayPauseClick(note.path.toString(), position) { stopPos -> showPlayIcon(holder) + (notes[position] as VoiceNote).isPlaying = false + notifyItemChanged(position) } handleMediaPlayerSideEffect(observeItemSideEffect(), holder) + note.isPlaying = !note.isPlaying } + + if (note.isPlaying) { + showPauseIcon(holder) + } else { + showPlayIcon(holder) + } + } else if (note is GraphicNote) { holder.canvasGraphicThumb.visible() onThumbPrepare(note, holder.canvasGraphicThumb) diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkRecorderFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkRecorderFragment.kt index c3dfc44a..27320ea4 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkRecorderFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/ArkRecorderFragment.kt @@ -421,6 +421,13 @@ class ArkRecorderFragment: BaseEditNoteFragment() { } } + override fun onStop() { + super.onStop() + if (mediaPlayViewModel.isPlaying()) { + mediaPlayViewModel.onPlayOrPauseClick(getCurrentRecordingPath()) + } + } + override fun onDestroy() { super.onDestroy() val view = activity.window?.decorView ?: return diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/NotesFragment.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/NotesFragment.kt index 6e6eaab6..84d2bd55 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/NotesFragment.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/fragments/NotesFragment.kt @@ -18,6 +18,7 @@ import dagger.hilt.android.AndroidEntryPoint import dev.arkbuilders.arkmemo.R import dev.arkbuilders.arkmemo.databinding.FragmentHomeBinding import dev.arkbuilders.arkmemo.models.Note +import dev.arkbuilders.arkmemo.models.VoiceNote import dev.arkbuilders.arkmemo.ui.activities.MainActivity import dev.arkbuilders.arkmemo.ui.adapters.NotesListAdapter import dev.arkbuilders.arkmemo.ui.dialogs.CommonActionDialog @@ -45,6 +46,8 @@ class NotesFragment: Fragment() { private var notesAdapter: NotesListAdapter? = null private var showingFloatingButtons = false + private var playingAudioPath: String? = null + private var playingAudioPosition = -1 private val newTextNoteClickListener = View.OnClickListener { onFloatingActionButtonClicked() @@ -158,6 +161,8 @@ class NotesFragment: Fragment() { notesAdapter = NotesListAdapter( notes, onPlayPauseClick = { path, pos, onStop -> + playingAudioPath = path + playingAudioPosition = pos ?: -1 arkMediaPlayerViewModel.onPlayOrPauseClick(path, pos, onStop) }, onThumbPrepare = { graphicNote, noteCanvas -> @@ -198,6 +203,17 @@ class NotesFragment: Fragment() { } } + override fun onStop() { + super.onStop() + if (arkMediaPlayerViewModel.isPlaying()) { + playingAudioPath?.let { + arkMediaPlayerViewModel.onPlayOrPauseClick(it) + (notesAdapter?.getNotes()?.getOrNull(playingAudioPosition) as? VoiceNote)?.isPlaying = false + notesAdapter?.notifyItemChanged(playingAudioPosition) + } + } + } + override fun onResume() { super.onResume() activity.fragment = this diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/ArkMediaPlayerViewModel.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/ArkMediaPlayerViewModel.kt index 0a4a1005..a37767db 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/ArkMediaPlayerViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/viewmodels/ArkMediaPlayerViewModel.kt @@ -152,4 +152,8 @@ class ArkMediaPlayerViewModel @Inject constructor( fun isPlayerInitialized(): Boolean{ return arkMediaPlayer.isInitialized() } + + fun isPlaying(): Boolean { + return arkMediaPlayer.isPlaying() + } } \ No newline at end of file