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:
Generated
-8
@@ -1,13 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module external.linked.project.id="Mear" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
<module external.linked.project.id="Mear" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
||||||
<component name="FacetManager">
|
|
||||||
<facet type="java-gradle" name="Java-Gradle">
|
|
||||||
<configuration>
|
|
||||||
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
|
|
||||||
<option name="BUILDABLE" value="false" />
|
|
||||||
</configuration>
|
|
||||||
</facet>
|
|
||||||
</component>
|
|
||||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
|
|||||||
@@ -6,9 +6,11 @@ import android.graphics.BitmapFactory
|
|||||||
import android.media.MediaPlayer
|
import android.media.MediaPlayer
|
||||||
import android.media.MediaMetadataRetriever
|
import android.media.MediaMetadataRetriever
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.os.Handler
|
||||||
import android.support.v7.app.AppCompatActivity
|
import android.support.v7.app.AppCompatActivity
|
||||||
|
|
||||||
import java.lang.Exception
|
import java.lang.Exception
|
||||||
|
import java.lang.Runnable
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
import kotlinx.android.synthetic.main.fragment_play_controls.*
|
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.R
|
||||||
import com.example.mear.repositories.TrackRepository
|
import com.example.mear.repositories.TrackRepository
|
||||||
import org.jetbrains.anko.image
|
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
@@ -38,6 +39,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private fun initialize() {
|
private fun initialize() {
|
||||||
songCount = TrackRepository(this).getSongCount()
|
songCount = TrackRepository(this).getSongCount()
|
||||||
currentSong = fetchSongIndex(PlayTypes.PlaySong)
|
currentSong = fetchSongIndex(PlayTypes.PlaySong)
|
||||||
|
TrackElapsing.progress = 0
|
||||||
|
TrackElapsing.max = 100
|
||||||
initializeMediaPlayer()
|
initializeMediaPlayer()
|
||||||
|
|
||||||
initializeClickListeners()
|
initializeClickListeners()
|
||||||
@@ -162,40 +165,50 @@ class MainActivity : AppCompatActivity() {
|
|||||||
PreviousTrack.isEnabled = true
|
PreviousTrack.isEnabled = true
|
||||||
}
|
}
|
||||||
private fun configureTrackDisplay() {
|
private fun configureTrackDisplay() {
|
||||||
configurePlayControlsDisplay()
|
try {
|
||||||
val currTrack = TrackRepository(this).getTrack(currentSong!!)
|
configurePlayControlsDisplay()
|
||||||
val trackTitle = currTrack.title
|
val currTrack = TrackRepository(this).getTrack(currentSong!!)
|
||||||
val artistTitle = currTrack.artist
|
val trackTitle = currTrack.title
|
||||||
val albumTitle = currTrack.album
|
val artistTitle = currTrack.artist
|
||||||
val trackDuration = currTrack.length
|
val albumTitle = currTrack.album
|
||||||
var trackCover: ByteArray? = null
|
val trackDuration = currTrack.length
|
||||||
val dur = String.format("%02d:%02d", TimeUnit.SECONDS.toMinutes(trackDuration.toLong()),
|
var trackCover: ByteArray? = null
|
||||||
(trackDuration % 60)
|
val dur = String.format(
|
||||||
|
"%02d:%02d", TimeUnit.SECONDS.toMinutes(trackDuration.toLong()),
|
||||||
|
(trackDuration % 60)
|
||||||
)
|
)
|
||||||
|
|
||||||
val mmr = MediaMetadataRetriever()
|
val mmr = MediaMetadataRetriever()
|
||||||
mmr.setDataSource(currTrack.songPath)
|
mmr.setDataSource(currTrack.songPath)
|
||||||
|
|
||||||
if (mmr.embeddedPicture != null) {
|
if (mmr.embeddedPicture != null) {
|
||||||
trackCover = mmr.embeddedPicture
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (ex: Exception) {
|
||||||
TrackTitle.text = null
|
val msg = ex.message
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private fun updateTrackProgress() {
|
||||||
|
musicHandler!!.postDelayed(musicTrackTimeUpdateTask, 100)
|
||||||
|
}
|
||||||
private fun configurePlayControlsDisplay() {
|
private fun configurePlayControlsDisplay() {
|
||||||
PlayTrack.background = null
|
PlayTrack.background = null
|
||||||
PlayTrack.colorFilter = null
|
PlayTrack.colorFilter = null
|
||||||
@@ -250,7 +263,25 @@ class MainActivity : AppCompatActivity() {
|
|||||||
return songIndex!!
|
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 trackPlayer: MediaPlayer? = null
|
||||||
private var currentSong: Int? = null
|
private var currentSong: Int? = null
|
||||||
private var playerInitialized: Boolean? = false
|
private var playerInitialized: Boolean? = false
|
||||||
|
|||||||
@@ -5,21 +5,23 @@
|
|||||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||||
|
|
||||||
<SeekBar
|
<SeekBar
|
||||||
android:id="@+id/seekBar2"
|
android:id="@+id/TrackElapsing"
|
||||||
android:layout_width="284dp"
|
android:layout_width="288dp"
|
||||||
android:layout_height="26dp"
|
android:layout_height="26dp"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
android:layout_marginTop="8dp"
|
android:layout_marginTop="8dp"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:layout_marginBottom="8dp"
|
android:layout_marginBottom="8dp"
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toStartOf="@+id/TrackDuration"
|
app:layout_constraintEnd_toStartOf="@+id/TrackDuration"
|
||||||
|
app:layout_constraintStart_toEndOf="@+id/CurrentPosition"
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
app:layout_constraintVertical_bias="0.869" />
|
app:layout_constraintVertical_bias="0.869" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/CurrentPosition"
|
android:id="@+id/CurrentPosition"
|
||||||
android:layout_width="50dp"
|
android:layout_width="48dp"
|
||||||
android:layout_height="22dp"
|
android:layout_height="21dp"
|
||||||
android:layout_marginStart="4dp"
|
android:layout_marginStart="4dp"
|
||||||
android:layout_marginBottom="84dp"
|
android:layout_marginBottom="84dp"
|
||||||
android:text="@string/track_start"
|
android:text="@string/track_start"
|
||||||
@@ -29,8 +31,8 @@
|
|||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/TrackDuration"
|
android:id="@+id/TrackDuration"
|
||||||
android:layout_width="50dp"
|
android:layout_width="48dp"
|
||||||
android:layout_height="22dp"
|
android:layout_height="21dp"
|
||||||
android:layout_marginEnd="4dp"
|
android:layout_marginEnd="4dp"
|
||||||
android:layout_marginBottom="84dp"
|
android:layout_marginBottom="84dp"
|
||||||
android:text="@string/track_end"
|
android:text="@string/track_end"
|
||||||
|
|||||||
Reference in New Issue
Block a user