-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPlayViewModel.kt
More file actions
204 lines (158 loc) · 5.39 KB
/
Copy pathPlayViewModel.kt
File metadata and controls
204 lines (158 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.bluehub.mixi.screens.mixing
import android.content.Context
import androidx.lifecycle.*
import com.bluehub.mixi.common.utils.areEqual
import com.bluehub.mixi.common.utils.reInitList
import com.bluehub.mixi.common.viewmodel.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*
import javax.inject.Inject
@HiltViewModel
class PlayViewModel @Inject constructor(
@ApplicationContext val context: Context,
private val mixingRepository: MixingRepository,
private val audioFileStore: AudioFileStore,
private val playFlagStore: PlayFlagStore) : BaseViewModel() {
lateinit var mAudioFilePath: String
val isPlaying: LiveData<Boolean>
get() = playFlagStore.isPlaying
val isGroupPlaying: LiveData<Boolean>
get() = playFlagStore.isGroupPlaying
private lateinit var _isLoading: MutableLiveData<Boolean>
private val playList: MutableList<String> = mutableListOf()
private var seekbarTimer: Timer? = null
private val _seekbarProgress = MutableLiveData<Int>(0)
val seekbarProgress: LiveData<Int>
get() = _seekbarProgress
private val _seekbarMaxValue = MutableLiveData<Int>(0)
val seekbarMaxValue: LiveData<Int>
get() = _seekbarMaxValue
fun setIsLoadingLiveData(isLoading: MutableLiveData<Boolean>) {
_isLoading = isLoading
}
fun setAudioFilePath(audioFilePath: String) {
mAudioFilePath = audioFilePath
loadSource()
}
fun togglePlay() {
if (isPlaying.value == null || isPlaying.value == false) {
playAudio()
} else {
pauseAudio()
}
}
fun toggleGroupPlay() {
if (isGroupPlaying.value == null || isGroupPlaying.value == false) {
groupPlay()
} else {
groupPause()
}
}
private fun loadSource() {
viewModelScope.launch(Dispatchers.IO) {
setIsLoading()
val pathList = listOf(mAudioFilePath)
mixingRepository.loadFiles(pathList)
_seekbarMaxValue.postValue(mixingRepository.getTotalSampleFrames())
cancelIsLoading()
}
}
private fun playAudio() {
viewModelScope.launch(Dispatchers.IO) {
setIsLoading()
val pathList = listOf(mAudioFilePath)
if (!playList.areEqual(pathList)) {
mixingRepository.loadFiles(pathList)
playList.reInitList(pathList)
}
mixingRepository.startPlayback()
playFlagStore.isPlaying.postValue(true)
cancelIsLoading()
}
}
private fun pauseAudio() {
viewModelScope.launch(Dispatchers.IO) {
setIsLoading()
mixingRepository.pausePlayback()
playFlagStore.isPlaying.postValue(false)
cancelIsLoading()
}
}
private fun groupPlay() {
viewModelScope.launch(Dispatchers.IO) {
setIsLoading()
val pathList = audioFileStore.audioFiles.map { it.path }
if (!playList.areEqual(pathList)) {
mixingRepository.loadFiles(pathList)
playList.reInitList(pathList)
}
mixingRepository.startPlayback()
playFlagStore.isGroupPlaying.postValue(true)
cancelIsLoading()
}
}
private fun groupPause() {
viewModelScope.launch(Dispatchers.IO) {
setIsLoading()
mixingRepository.pausePlayback()
playFlagStore.isGroupPlaying.postValue(false)
cancelIsLoading()
}
}
fun startPlayback() {
viewModelScope.launch(Dispatchers.IO) {
setIsLoading()
mixingRepository.startPlayback()
cancelIsLoading()
}
}
fun pausePlayback() {
viewModelScope.launch(Dispatchers.IO) {
setIsLoading()
mixingRepository.pausePlayback()
cancelIsLoading()
}
}
fun startTrackingSeekbarTimer() {
_seekbarProgress.value = 0
_seekbarMaxValue.value = mixingRepository.getTotalSampleFrames()
stopTrackingSeekbarTimer()
seekbarTimer = Timer()
seekbarTimer?.schedule(object: TimerTask() {
override fun run() {
_seekbarProgress.postValue(mixingRepository.getCurrentPlaybackProgress())
}
}, 0, 10)
}
fun stopTrackingSeekbarTimer() {
seekbarTimer?.cancel()
seekbarTimer = null
}
fun setPlayerHead(playHead: Int) = mixingRepository.setPlayerHead(playHead)
fun setTrackPlayHead(playHead: Int) = mixingRepository.setSourcePlayHead(mAudioFilePath, playHead)
fun setIsLoading() {
if (::_isLoading.isInitialized) {
_isLoading.postValue(true)
}
}
fun cancelIsLoading() {
if (::_isLoading.isInitialized) {
_isLoading.postValue(false)
}
}
override fun onCleared() {
super.onCleared()
if (isPlaying.value == true || isGroupPlaying.value == true) {
mixingRepository.pausePlayback()
}
if (isPlaying.value == true) {
playFlagStore.isPlaying.value = false
}
if (isGroupPlaying.value == true) {
playFlagStore.isGroupPlaying.value = false
}
}
}