Now the track's current time position as well as the the progress of the song is being displayed #24 #25
This commit is contained in:
@@ -6,9 +6,11 @@ import android.graphics.BitmapFactory
|
||||
import android.media.MediaPlayer
|
||||
import android.media.MediaMetadataRetriever
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
|
||||
import java.lang.Exception
|
||||
import java.lang.Runnable
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.fragment_play_controls.*
|
||||
@@ -21,7 +23,6 @@ import kotlin.random.Random
|
||||
|
||||
import com.example.mear.R
|
||||
import com.example.mear.repositories.TrackRepository
|
||||
import org.jetbrains.anko.image
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
@@ -38,6 +39,8 @@ class MainActivity : AppCompatActivity() {
|
||||
private fun initialize() {
|
||||
songCount = TrackRepository(this).getSongCount()
|
||||
currentSong = fetchSongIndex(PlayTypes.PlaySong)
|
||||
TrackElapsing.progress = 0
|
||||
TrackElapsing.max = 100
|
||||
initializeMediaPlayer()
|
||||
|
||||
initializeClickListeners()
|
||||
@@ -162,40 +165,50 @@ class MainActivity : AppCompatActivity() {
|
||||
PreviousTrack.isEnabled = true
|
||||
}
|
||||
private fun configureTrackDisplay() {
|
||||
configurePlayControlsDisplay()
|
||||
val currTrack = TrackRepository(this).getTrack(currentSong!!)
|
||||
val trackTitle = currTrack.title
|
||||
val artistTitle = currTrack.artist
|
||||
val albumTitle = currTrack.album
|
||||
val trackDuration = currTrack.length
|
||||
var trackCover: ByteArray? = null
|
||||
val dur = String.format("%02d:%02d", TimeUnit.SECONDS.toMinutes(trackDuration.toLong()),
|
||||
(trackDuration % 60)
|
||||
try {
|
||||
configurePlayControlsDisplay()
|
||||
val currTrack = TrackRepository(this).getTrack(currentSong!!)
|
||||
val trackTitle = currTrack.title
|
||||
val artistTitle = currTrack.artist
|
||||
val albumTitle = currTrack.album
|
||||
val trackDuration = currTrack.length
|
||||
var trackCover: ByteArray? = null
|
||||
val dur = String.format(
|
||||
"%02d:%02d", TimeUnit.SECONDS.toMinutes(trackDuration.toLong()),
|
||||
(trackDuration % 60)
|
||||
)
|
||||
|
||||
val mmr = MediaMetadataRetriever()
|
||||
mmr.setDataSource(currTrack.songPath)
|
||||
val mmr = MediaMetadataRetriever()
|
||||
mmr.setDataSource(currTrack.songPath)
|
||||
|
||||
if (mmr.embeddedPicture != null) {
|
||||
trackCover = mmr.embeddedPicture
|
||||
if (mmr.embeddedPicture != null) {
|
||||
trackCover = mmr.embeddedPicture
|
||||
}
|
||||
updateTrackProgress()
|
||||
|
||||
TrackTitle.text = null
|
||||
ArtistTitle.text = null
|
||||
AlbumTitle.text = null
|
||||
CurrentPosition.text = null
|
||||
TrackCover.setImageBitmap(null)
|
||||
|
||||
TrackTitle.text = trackTitle
|
||||
ArtistTitle.text = artistTitle
|
||||
AlbumTitle.text = albumTitle
|
||||
TrackDuration.text = dur
|
||||
if (trackCover != null) {
|
||||
val songImage = BitmapFactory
|
||||
.decodeByteArray(trackCover, 0, trackCover.size)
|
||||
TrackCover.setImageBitmap(songImage)
|
||||
}
|
||||
}
|
||||
|
||||
TrackTitle.text = null
|
||||
ArtistTitle.text = null
|
||||
AlbumTitle.text = null
|
||||
CurrentPosition.text = null
|
||||
TrackCover.setImageBitmap(null)
|
||||
|
||||
TrackTitle.text = trackTitle
|
||||
ArtistTitle.text = artistTitle
|
||||
AlbumTitle.text = albumTitle
|
||||
TrackDuration.text = dur
|
||||
if (trackCover != null) {
|
||||
val songImage = BitmapFactory
|
||||
.decodeByteArray(trackCover, 0, trackCover.size)
|
||||
TrackCover.setImageBitmap(songImage)
|
||||
catch (ex: Exception) {
|
||||
val msg = ex.message
|
||||
}
|
||||
}
|
||||
private fun updateTrackProgress() {
|
||||
musicHandler!!.postDelayed(musicTrackTimeUpdateTask, 100)
|
||||
}
|
||||
private fun configurePlayControlsDisplay() {
|
||||
PlayTrack.background = null
|
||||
PlayTrack.colorFilter = null
|
||||
@@ -250,7 +263,25 @@ class MainActivity : AppCompatActivity() {
|
||||
return songIndex!!
|
||||
}
|
||||
|
||||
private var musicTrackTimeUpdateTask = object: Runnable {
|
||||
override fun run() {
|
||||
var newPosition = 0
|
||||
val currentPosition = trackPlayer!!.currentPosition / 1000
|
||||
val totalDuration = trackPlayer!!.duration / 1000
|
||||
newPosition = (((currentPosition).toDouble() / totalDuration) * 100).toInt()
|
||||
val dur = String.format(
|
||||
"%02d:%02d", TimeUnit.SECONDS.toMinutes(currentPosition.toLong()),
|
||||
(currentPosition % 60)
|
||||
)
|
||||
CurrentPosition.text = dur
|
||||
|
||||
TrackElapsing.progress = newPosition
|
||||
|
||||
musicHandler!!.postDelayed(this, 100)
|
||||
}
|
||||
}
|
||||
|
||||
private var musicHandler: Handler? = Handler()
|
||||
private var trackPlayer: MediaPlayer? = null
|
||||
private var currentSong: Int? = null
|
||||
private var playerInitialized: Boolean? = false
|
||||
|
||||
@@ -5,21 +5,23 @@
|
||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||
|
||||
<SeekBar
|
||||
android:id="@+id/seekBar2"
|
||||
android:layout_width="284dp"
|
||||
android:id="@+id/TrackElapsing"
|
||||
android:layout_width="288dp"
|
||||
android:layout_height="26dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/TrackDuration"
|
||||
app:layout_constraintStart_toEndOf="@+id/CurrentPosition"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintVertical_bias="0.869" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/CurrentPosition"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="22dp"
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="21dp"
|
||||
android:layout_marginStart="4dp"
|
||||
android:layout_marginBottom="84dp"
|
||||
android:text="@string/track_start"
|
||||
@@ -29,8 +31,8 @@
|
||||
|
||||
<TextView
|
||||
android:id="@+id/TrackDuration"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="22dp"
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="21dp"
|
||||
android:layout_marginEnd="4dp"
|
||||
android:layout_marginBottom="84dp"
|
||||
android:text="@string/track_end"
|
||||
|
||||
Reference in New Issue
Block a user