add handling for play/pause

This commit is contained in:
tibbi 2020-04-01 17:00:45 +02:00
parent 0796147fee
commit 14d8511b7b
2 changed files with 39 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import android.os.PowerManager
import android.provider.MediaStore import android.provider.MediaStore
import android.util.AttributeSet import android.util.AttributeSet
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity import com.simplemobiletools.voicerecorder.activities.SimpleActivity
import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter
import com.simplemobiletools.voicerecorder.models.Recording import com.simplemobiletools.voicerecorder.models.Recording
@ -52,6 +53,10 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
} }
initMediaPlayer() initMediaPlayer()
play_pause_btn.setOnClickListener {
togglePlayPause()
}
} }
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
@ -120,6 +125,11 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
start() start()
} }
songStateChanged(true)
setupProgressTimer()
}
private fun setupProgressTimer() {
progressTimer.cancel() progressTimer.cancel()
progressTimer = Timer() progressTimer = Timer()
progressTimer.scheduleAtFixedRate(getProgressUpdateTask(), 1000, 1000) progressTimer.scheduleAtFixedRate(getProgressUpdateTask(), 1000, 1000)
@ -130,8 +140,8 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
if (player != null) { if (player != null) {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
val progress = player!!.currentPosition / 1000 val progress = player!!.currentPosition / 1000
updateCurrentProgress(progress)
player_progressbar.progress = progress player_progressbar.progress = progress
player_progress_current.text = progress.getFormattedDuration()
} }
} }
} }
@ -141,6 +151,31 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
player_progress_current.text = seconds.getFormattedDuration() player_progress_current.text = seconds.getFormattedDuration()
} }
private fun togglePlayPause() {
if (getIsPlaying()) {
pauseSong()
} else {
resumeSong()
}
}
private fun pauseSong() {
player?.pause()
songStateChanged(false)
}
private fun resumeSong() {
player?.start()
songStateChanged(true)
}
private fun songStateChanged(isPlaying: Boolean) {
val drawable = resources.getDrawable(if (isPlaying) R.drawable.ic_pause_vector else R.drawable.ic_play_vector)
play_pause_btn.setImageDrawable(drawable)
}
private fun getIsPlaying() = player?.isPlaying == true
private fun setupColors() { private fun setupColors() {
recordings_fastscroller.updatePrimaryColor() recordings_fastscroller.updatePrimaryColor()
recordings_fastscroller.updateBubbleColors() recordings_fastscroller.updateBubbleColors()

View File

@ -106,7 +106,7 @@
android:layout_width="@dimen/normal_icon_size" android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size" android:layout_height="@dimen/normal_icon_size"
android:layout_weight="1" android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless" android:background="?attr/selectableItemBackground"
android:contentDescription="@string/previous" android:contentDescription="@string/previous"
android:paddingTop="@dimen/small_margin" android:paddingTop="@dimen/small_margin"
android:paddingBottom="@dimen/small_margin" android:paddingBottom="@dimen/small_margin"
@ -117,7 +117,7 @@
android:layout_width="@dimen/normal_icon_size" android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size" android:layout_height="@dimen/normal_icon_size"
android:layout_weight="1" android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless" android:background="?attr/selectableItemBackground"
android:contentDescription="@string/playpause" android:contentDescription="@string/playpause"
android:paddingTop="@dimen/small_margin" android:paddingTop="@dimen/small_margin"
android:paddingBottom="@dimen/small_margin" android:paddingBottom="@dimen/small_margin"
@ -128,7 +128,7 @@
android:layout_width="@dimen/normal_icon_size" android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size" android:layout_height="@dimen/normal_icon_size"
android:layout_weight="1" android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless" android:background="?attr/selectableItemBackground"
android:contentDescription="@string/next" android:contentDescription="@string/next"
android:paddingTop="@dimen/small_margin" android:paddingTop="@dimen/small_margin"
android:paddingBottom="@dimen/small_margin" android:paddingBottom="@dimen/small_margin"