Skip to content

Commit

Permalink
Stop Audio playback when view is no longer visible
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Aug 8, 2024
1 parent 03e414f commit 8e40cb7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,8 @@ class ArkMediaPlayerViewModel @Inject constructor(
fun isPlayerInitialized(): Boolean{
return arkMediaPlayer.isInitialized()
}

fun isPlaying(): Boolean {
return arkMediaPlayer.isPlaying()
}
}

0 comments on commit 8e40cb7

Please sign in to comment.