Able to play music, next song, previous song
This commit is contained in:
+223
File diff suppressed because one or more lines are too long
@@ -29,6 +29,13 @@ dependencies {
|
||||
implementation 'com.android.support:support-v4:28.0.0'
|
||||
implementation 'com.android.support:design:28.0.0'
|
||||
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
||||
implementation 'org.jetbrains.anko:anko-common:0.9'
|
||||
implementation 'com.ealva:ealvatag:0.4.2'
|
||||
implementation 'com.ealva:ealvalog:0.3.0'
|
||||
implementation 'com.ealva:ealvalog-core:0.3.0'
|
||||
implementation 'com.ealva:ealvalog-android:0.3.0'
|
||||
implementation 'com.google.guava:guava:20.0'
|
||||
implementation 'com.squareup.okio:okio:1.11.0'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
package com.example.mear
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.media.MediaPlayer
|
||||
import android.os.Bundle
|
||||
import android.os.Environment
|
||||
import android.support.design.widget.Snackbar
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import java.lang.Exception
|
||||
import kotlinx.android.synthetic.main.activity_main.*
|
||||
import kotlinx.android.synthetic.main.content_main.*
|
||||
import java.io.File
|
||||
import java.lang.Exception
|
||||
import java.nio.file.Files
|
||||
import kotlin.io.*
|
||||
import kotlin.random.Random
|
||||
|
||||
import org.jetbrains.anko.*
|
||||
|
||||
import com.example.mear.management.MusicFiles
|
||||
import kotlin.random.Random
|
||||
import com.example.mear.management.TrackManager
|
||||
import com.example.mear.models.Track
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@@ -23,54 +27,42 @@ class MainActivity : AppCompatActivity() {
|
||||
setContentView(R.layout.activity_main)
|
||||
setSupportActionBar(toolbar)
|
||||
|
||||
loadSongs()
|
||||
loadTracks(loadSongPaths())
|
||||
|
||||
initializeMediaPlayer()
|
||||
|
||||
PlayTrack.setOnClickListener {
|
||||
playSongTrack()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun configureSongs() {
|
||||
|
||||
}
|
||||
fun configureDemoSong() {
|
||||
val sdC = "sdcard0/"
|
||||
var musicPath = "music/"
|
||||
var artistPath = "Flueric/"
|
||||
var albumPath = "New Death of a Phoenix/"
|
||||
var songPath = "Qualm.mp3"
|
||||
val pa = demoPath!!.absoluteFile.toString() + "/" + musicPath + artistPath + albumPath +
|
||||
songPath
|
||||
try {
|
||||
var fl = File(pa)
|
||||
if (fl.exists()) {
|
||||
TrackPayer!!.setDataSource(pa)
|
||||
}
|
||||
NextTrack.setOnClickListener {
|
||||
playNextSongTrack()
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
var exMsg = ex.message
|
||||
PreviousTrack.setOnClickListener {
|
||||
playPreviousSongTrack()
|
||||
}
|
||||
TrackPayer!!.setOnCompletionListener {
|
||||
playNextSongTrack()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun initializeMediaPlayer() {
|
||||
private fun initializeMediaPlayer() {
|
||||
if (TrackPayer == null) {
|
||||
TrackPayer = MediaPlayer()
|
||||
playerInitialized = true
|
||||
TrackPayer!!.setDataSource(allSongs!![currentSong!!])
|
||||
TrackPayer!!.setDataSource(allTracks!![currentSong!!].songPath)
|
||||
TrackPayer!!.prepare()
|
||||
}
|
||||
}
|
||||
|
||||
fun playSongTrack() {
|
||||
private fun playSongTrack() {
|
||||
|
||||
try {
|
||||
if (!TrackPayer!!.isPlaying) {
|
||||
TrackPayer!!.start()
|
||||
var musicData = TrackPayer!!.trackInfo
|
||||
configureTrackDisplay()
|
||||
|
||||
|
||||
println("ss")
|
||||
} else {
|
||||
TrackPayer!!.pause()
|
||||
@@ -80,19 +72,120 @@ class MainActivity : AppCompatActivity() {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
}
|
||||
private fun playNextSongTrack() {
|
||||
try {
|
||||
currentSong = fetchSongIndex(PlayTypes.PlayNextSong)
|
||||
|
||||
if (TrackPayer!!.isPlaying) {
|
||||
TrackPayer!!.stop()
|
||||
}
|
||||
|
||||
private fun loadSongs() {
|
||||
val mfPaths = MusicFiles(this.demoPath!!)
|
||||
TrackPayer!!.reset()
|
||||
TrackPayer!!.setDataSource(allTracks!![currentSong!!].songPath)
|
||||
TrackPayer!!.prepare()
|
||||
TrackPayer!!.start()
|
||||
configureTrackDisplay()
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
print(exMsg)
|
||||
}
|
||||
}
|
||||
private fun playPreviousSongTrack() {
|
||||
try {
|
||||
currentSong = fetchSongIndex(PlayTypes.PlayPreviousSong)
|
||||
|
||||
if (TrackPayer!!.isPlaying) {
|
||||
TrackPayer!!.stop()
|
||||
}
|
||||
|
||||
TrackPayer!!.reset()
|
||||
TrackPayer!!.setDataSource(allTracks!![currentSong!!].songPath)
|
||||
TrackPayer!!.prepare()
|
||||
TrackPayer!!.start()
|
||||
configureTrackDisplay()
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
print(exMsg)
|
||||
}
|
||||
}
|
||||
private fun configureTrackDisplay() {
|
||||
val trackTitle = allTracks!![currentSong!!].title
|
||||
val albumTitle = allTracks!![currentSong!!].album
|
||||
var trackCover: ByteArray? = null
|
||||
|
||||
if (!(allTracks!![currentSong!!].TrackCover == null)) {
|
||||
trackCover = allTracks!![currentSong!!].TrackCover
|
||||
}
|
||||
|
||||
TrackTitle.text.clear()
|
||||
AlbumTitle.text.clear()
|
||||
TrackCover.setImageBitmap(null)
|
||||
|
||||
TrackTitle.setText(trackTitle)
|
||||
AlbumTitle.setText(albumTitle)
|
||||
if (trackCover != null) {
|
||||
var songImage = BitmapFactory
|
||||
.decodeByteArray(trackCover, 0, trackCover!!.size)
|
||||
TrackCover.setImageBitmap(songImage)
|
||||
}
|
||||
}
|
||||
|
||||
private fun fetchSongIndex(playType: PlayTypes): Int {
|
||||
var songIndex: Int? = null
|
||||
|
||||
try {
|
||||
when (playType) {
|
||||
PlayTypes.PlayPreviousSong -> {
|
||||
if (currentSong!! == 0) {
|
||||
songIndex = allTracks!!.size - 1
|
||||
} else {
|
||||
songIndex = currentSong!!.dec()
|
||||
}
|
||||
}
|
||||
PlayTypes.PlaySong -> {
|
||||
songIndex = Random.nextInt(0, allTracks!!.size - 1)
|
||||
}
|
||||
PlayTypes.PlayNextSong -> {
|
||||
if (currentSong!! == (allTracks!!.size - 1)) {
|
||||
songIndex = 0
|
||||
} else {
|
||||
songIndex = currentSong!!.inc()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
println(exMsg)
|
||||
}
|
||||
|
||||
return songIndex!!
|
||||
}
|
||||
|
||||
private fun loadSongPaths(): MutableList<String> {
|
||||
val demoPath = Environment.getExternalStorageDirectory()
|
||||
val mfPaths = MusicFiles(demoPath)
|
||||
mfPaths.loadAllMusicPaths()
|
||||
allSongs = mfPaths.allSongs
|
||||
val songIndex = Random.nextInt(0, allSongs!!.size - 1)
|
||||
currentSong = songIndex
|
||||
val allSongs = mfPaths.allSongs
|
||||
|
||||
return allSongs!!
|
||||
}
|
||||
private fun loadTracks(allSongs: MutableList<String>) {
|
||||
val trackMgr = TrackManager(allSongs!!)
|
||||
trackMgr.configureTracks()
|
||||
allTracks = trackMgr.allTracks
|
||||
|
||||
currentSong = fetchSongIndex(PlayTypes.PlaySong)
|
||||
}
|
||||
|
||||
private var TrackPayer: MediaPlayer? = null
|
||||
private var demoPath: File? = Environment .getExternalStorageDirectory()
|
||||
private var allSongs: MutableList<String>? = null
|
||||
private var allTracks: MutableList<Track>? = null
|
||||
private var currentSong: Int? = null
|
||||
private var playerInitialized: Boolean? = null
|
||||
|
||||
private enum class PlayTypes {
|
||||
PlayNextSong, PlaySong, PlayPreviousSong
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,21 +19,25 @@ class MusicFiles (val demoPath: File) {
|
||||
val listOfFiles = folder.listFiles()
|
||||
|
||||
for (i in listOfFiles) {
|
||||
for (j in i.listFiles()) {
|
||||
if (j.absolutePath.toString().contains("zip")) {
|
||||
println("zip file")
|
||||
}
|
||||
for (k in j.listFiles()) {
|
||||
println("What's good?")
|
||||
if (k.absolutePath.toString().contains("mp3")) {
|
||||
allSongs!!.add(k.absolutePath.toString())
|
||||
count++
|
||||
if (allSongs!!.count() < musicSongLimit) {
|
||||
for (j in i.listFiles()) {
|
||||
if (j.absolutePath.toString().contains("zip")) {
|
||||
println("zip file")
|
||||
}
|
||||
else {
|
||||
println("Stranger")
|
||||
for (k in j.listFiles()) {
|
||||
println("What's good?")
|
||||
if (k.absolutePath.toString().contains("mp3")) {
|
||||
allSongs!!.add(k.absolutePath.toString())
|
||||
count++
|
||||
} else {
|
||||
println("Stranger")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
break
|
||||
}
|
||||
}
|
||||
songCount = count
|
||||
}
|
||||
@@ -41,25 +45,9 @@ class MusicFiles (val demoPath: File) {
|
||||
var exMsg = ex.message
|
||||
}
|
||||
}
|
||||
fun configureDemoSong() {
|
||||
val sdC = "sdcard0/"
|
||||
var musicPath = "music/"
|
||||
var artistPath = "Flueric/"
|
||||
var albumPath = "New Death of a Phoenix/"
|
||||
var songPath = "Qualm.mp3"
|
||||
val pa = demoPath.absoluteFile.toString() + "/" + musicPath + artistPath + albumPath +
|
||||
songPath
|
||||
try {
|
||||
var fl = File(pa)
|
||||
if (fl.exists()) {
|
||||
}
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
var exMsg = ex.message
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var allSongs: MutableList<String>?= null
|
||||
var songCount: Int? = null
|
||||
val musicSongLimit = 50
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.example.mear.management
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.media.MediaMetadataRetriever
|
||||
|
||||
import java.io.File
|
||||
import java.lang.Exception
|
||||
|
||||
import ealvatag.audio.AudioFile
|
||||
import ealvatag.audio.AudioFileIO
|
||||
import ealvatag.tag.FieldKey
|
||||
import ealvatag.tag.NullTag
|
||||
|
||||
import com.example.mear.models.Track
|
||||
|
||||
class TrackManager(val allSongPath: List<String>) {
|
||||
|
||||
|
||||
fun configureTracks() {
|
||||
try {
|
||||
var id = 0
|
||||
allTracks = mutableListOf()
|
||||
for (musicPath in allSongPath) {
|
||||
var mmr = MediaMetadataRetriever()
|
||||
mmr.setDataSource(musicPath)
|
||||
val trackTitle = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)
|
||||
val trackArtist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)
|
||||
val trackAlbum = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)
|
||||
var trackLength: Int? = null
|
||||
val trackLenghStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
|
||||
trackLength = (trackLenghStr.toInt()/1000)
|
||||
|
||||
var art: ByteArray? = null
|
||||
if (mmr.embeddedPicture==null) {
|
||||
art = ByteArray(0)
|
||||
}
|
||||
else {
|
||||
art = mmr.embeddedPicture
|
||||
var songImage = BitmapFactory
|
||||
.decodeByteArray(art, 0, art!!.size)
|
||||
}
|
||||
|
||||
var track = Track(id, trackTitle, trackArtist, trackAlbum, trackLength,
|
||||
art!!, musicPath)
|
||||
id++
|
||||
allTracks!!.add(track)
|
||||
}
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
}
|
||||
|
||||
var allTracks: MutableList<Track>? = null
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.example.mear.models
|
||||
|
||||
class Track (val id: Int, val title: String, val artist: String, val album: String,
|
||||
val TrackCover: ByteArray) {
|
||||
val length: Int, val TrackCover: ByteArray, val songPath: String) {
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
tools:layout_editor_absoluteX="0dp">
|
||||
<ImageView
|
||||
tools:srcCompat="@tools:sample/backgrounds/scenic[2]"
|
||||
android:id="@+id/imageView" android:layout_height="326dp"
|
||||
android:id="@+id/TrackCover" android:layout_height="326dp"
|
||||
android:layout_width="match_parent"/>
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
|
||||
Reference in New Issue
Block a user