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 5, 2024
1 parent 03e414f commit 939128e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 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 @@ -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 @@ -45,6 +45,7 @@ class NotesFragment: Fragment() {
private var notesAdapter: NotesListAdapter? = null

private var showingFloatingButtons = false
private var playingAudioPath: String? = null

private val newTextNoteClickListener = View.OnClickListener {
onFloatingActionButtonClicked()
Expand Down Expand Up @@ -158,6 +159,7 @@ class NotesFragment: Fragment() {
notesAdapter = NotesListAdapter(
notes,
onPlayPauseClick = { path, pos, onStop ->
playingAudioPath = path
arkMediaPlayerViewModel.onPlayOrPauseClick(path, pos, onStop)
},
onThumbPrepare = { graphicNote, noteCanvas ->
Expand Down Expand Up @@ -198,6 +200,11 @@ class NotesFragment: Fragment() {
}
}

override fun onStop() {
super.onStop()
playingAudioPath?.let { arkMediaPlayerViewModel.onPlayOrPauseClick(it) }
}

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 939128e

Please sign in to comment.