Implemented repeat functionality #23 and added functionality to change the current position of the song via the seekbar #27
This commit is contained in:
committed by
amazing-username
parent
3d7c293555
commit
6a71da1540
@@ -17,6 +17,8 @@ import android.widget.Toast
|
||||
import java.lang.Exception
|
||||
import java.lang.Runnable
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlin.io.*
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.fragment_play_controls.*
|
||||
@@ -28,11 +30,10 @@ import kotlinx.android.synthetic.main.fragment_track_flow.*
|
||||
import org.jetbrains.anko.image
|
||||
import org.jetbrains.anko.imageBitmap
|
||||
|
||||
import com.example.mear.listeners.TrackElaspingChange
|
||||
import com.example.mear.playback.service.MusicService
|
||||
import com.example.mear.R
|
||||
import com.example.mear.models.PlayControls
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import com.example.mear.R
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
@@ -88,6 +89,14 @@ class MainActivity : AppCompatActivity() {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
}
|
||||
private fun initializeChangeListeners() {
|
||||
val newListener = TrackElaspingChange(TrackElapsing)
|
||||
if (musicService != null) {
|
||||
newListener.musicService = musicService
|
||||
newListener.initialize()
|
||||
TrackElapsing.setOnSeekBarChangeListener(newListener)
|
||||
}
|
||||
}
|
||||
private fun initializeClickListeners() {
|
||||
PlayTrack.setOnClickListener {
|
||||
playSongTrack()
|
||||
@@ -280,19 +289,13 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private var musicTrackTimeUpdateTask = object: Runnable {
|
||||
override fun run() {
|
||||
|
||||
try {
|
||||
var newPosition = 0
|
||||
val currentPosition = musicService!!.currentPositionOfTrack() / 1000
|
||||
val totalDuration = musicService!!.durationOfTrack() / 1000
|
||||
newPosition = (((currentPosition).toDouble() / totalDuration) * 100).toInt()
|
||||
val dur = String.format(
|
||||
"%02d:%02d", TimeUnit.SECONDS.toMinutes(currentPosition.toLong()),
|
||||
(currentPosition % 60)
|
||||
)
|
||||
CurrentPosition.text = dur
|
||||
val dur = String.format( "%02d:%02d",
|
||||
TimeUnit.SECONDS.toMinutes(currentPosition.toLong()),
|
||||
(currentPosition % 60) )
|
||||
|
||||
TrackElapsing.progress = newPosition
|
||||
CurrentPosition.text = dur
|
||||
|
||||
if (TrackCover.image == null && musicService!!.isPlaying()) {
|
||||
configureTrackDisplay()
|
||||
@@ -313,6 +316,7 @@ class MainActivity : AppCompatActivity() {
|
||||
runBlocking {
|
||||
val demo = launch {
|
||||
musicService = (service as MusicService.LocalBinder).service
|
||||
initializeChangeListeners()
|
||||
}
|
||||
demo.start()
|
||||
}
|
||||
@@ -321,11 +325,6 @@ class MainActivity : AppCompatActivity() {
|
||||
configureTrackDisplay()
|
||||
}
|
||||
}
|
||||
|
||||
Toast.makeText(
|
||||
this@MainActivity, "Music Service Started",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(className: ComponentName) {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.example.mear.listeners
|
||||
|
||||
import android.os.Handler
|
||||
import android.widget.SeekBar
|
||||
|
||||
import java.lang.Exception
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
import com.example.mear.playback.service.MusicService
|
||||
import com.example.mear.util.ConvertTrackPosition
|
||||
|
||||
|
||||
class TrackElaspingChange(var seekBar: SeekBar?): SeekBar.OnSeekBarChangeListener {
|
||||
|
||||
var musicService: MusicService? = null
|
||||
var mHandler: Handler? = Handler()
|
||||
|
||||
override fun onStartTrackingTouch(seekBar: SeekBar?) {
|
||||
this.seekBar = seekBar
|
||||
}
|
||||
override fun onStopTrackingTouch(seekBar: SeekBar?) {
|
||||
this.seekBar = seekBar
|
||||
val newPositionPercentage = this.seekBar!!.progress
|
||||
var convertPosition = ConvertTrackPosition(newPositionPercentage)
|
||||
val newPosition = convertPosition.newPosition(musicService!!.durationOfTrack())
|
||||
musicService!!.goToPosition(newPosition)
|
||||
this.seekBar!!.progress = newPosition
|
||||
}
|
||||
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
|
||||
this.seekBar = seekBar
|
||||
|
||||
if (fromUser ){
|
||||
|
||||
mHandler!!.postDelayed( musicTrackTimeUpdateTask, 250)
|
||||
}
|
||||
}
|
||||
|
||||
fun initialize() {
|
||||
mHandler!!.postDelayed(musicTrackTimeUpdateTask, 250)
|
||||
}
|
||||
|
||||
private var musicTrackTimeUpdateTask = object: Runnable {
|
||||
override fun run() {
|
||||
try {
|
||||
var newPosition = 0
|
||||
val currentPosition = musicService!!.currentPositionOfTrack() / 1000
|
||||
val totalDuration = musicService!!.durationOfTrack() / 1000
|
||||
newPosition = (((currentPosition).toDouble() / totalDuration) * 100).toInt()
|
||||
val dur = String.format(
|
||||
"%02d:%02d", TimeUnit.SECONDS.toMinutes(currentPosition.toLong()),
|
||||
(currentPosition % 60)
|
||||
)
|
||||
|
||||
seekBar!!.progress = newPosition
|
||||
|
||||
mHandler!!.postDelayed(this, 250)
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,11 @@ import android.content.Intent
|
||||
import android.media.MediaPlayer
|
||||
import android.os.Binder
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
import android.widget.Toast
|
||||
|
||||
import java.lang.Exception
|
||||
import kotlin.random.Random
|
||||
|
||||
import com.example.mear.management.DatabaseManager
|
||||
import com.example.mear.management.MusicFiles
|
||||
import com.example.mear.management.TrackManager
|
||||
import com.example.mear.models.PlayControls
|
||||
@@ -65,7 +63,9 @@ class MusicService: Service() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun goToPosition(progress: Int) {
|
||||
trackPlayer!!.seekTo(progress)
|
||||
}
|
||||
fun playSongTrack() {
|
||||
try {
|
||||
trackPlayer!!.start()
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.mear.util
|
||||
|
||||
|
||||
class ConvertTrackPosition(val percentage: Int) {
|
||||
|
||||
fun newPosition(trackDuration: Int): Int {
|
||||
val newPosition = (trackDuration * (percentage.toDouble() / 100)).toInt()
|
||||
|
||||
return newPosition
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user