new: add PlayerViewModel to manage audio playback and session handling
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
|||||||
|
package fr.ajaury.gwenedeg.player.ui.viewmodel
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import fr.ajaury.gwenedeg.player.AudioPlayer
|
||||||
|
import fr.ajaury.gwenedeg.player.AudioSessionManager
|
||||||
|
|
||||||
|
class PlayerViewModel(
|
||||||
|
private val audioPlayer: AudioPlayer,
|
||||||
|
private val audioSessionManager: AudioSessionManager,
|
||||||
|
) : ViewModel() {
|
||||||
|
init {
|
||||||
|
audioSessionManager.activate()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun play(filePath: String) {
|
||||||
|
audioPlayer.load(filePath = filePath)
|
||||||
|
audioPlayer.play()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun pause() {
|
||||||
|
audioPlayer.pause()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun stop() {
|
||||||
|
audioPlayer.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCleared() {
|
||||||
|
super.onCleared()
|
||||||
|
audioSessionManager.deactivate()
|
||||||
|
audioPlayer.release()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user