Able to play and pause a songs
This commit is contained in:
committed by
amazing-username
parent
00535c3c8d
commit
2d23bdec56
@@ -9,8 +9,13 @@ import android.support.v7.app.AppCompatActivity;
|
||||
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 com.example.mear.management.MusicFiles
|
||||
import kotlin.random.Random
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@@ -18,6 +23,8 @@ class MainActivity : AppCompatActivity() {
|
||||
setContentView(R.layout.activity_main)
|
||||
setSupportActionBar(toolbar)
|
||||
|
||||
loadSongs()
|
||||
|
||||
initializeMediaPlayer()
|
||||
|
||||
PlayTrack.setOnClickListener {
|
||||
@@ -29,19 +36,63 @@ class MainActivity : AppCompatActivity() {
|
||||
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)
|
||||
}
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
var exMsg = ex.message
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun initializeMediaPlayer() {
|
||||
if (TrackPayer == null) {
|
||||
TrackPayer = MediaPlayer()
|
||||
playerInitialized = true
|
||||
TrackPayer!!.setDataSource(allSongs!![currentSong!!])
|
||||
TrackPayer!!.prepare()
|
||||
}
|
||||
File("/").walkTopDown().forEach { print(it) }
|
||||
}
|
||||
|
||||
fun playSongTrack() {
|
||||
|
||||
try {
|
||||
if (!TrackPayer!!.isPlaying) {
|
||||
TrackPayer!!.start()
|
||||
var musicData = TrackPayer!!.trackInfo
|
||||
println("ss")
|
||||
} else {
|
||||
TrackPayer!!.pause()
|
||||
}
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
val exMsg = ex.message
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var TrackPayer: MediaPlayer? = null
|
||||
var sysPath = Environment.getRootDirectory()
|
||||
private fun loadSongs() {
|
||||
val mfPaths = MusicFiles(this.demoPath!!)
|
||||
mfPaths.loadAllMusicPaths()
|
||||
allSongs = mfPaths.allSongs
|
||||
val songIndex = Random.nextInt(0, allSongs!!.size - 1)
|
||||
currentSong = songIndex
|
||||
}
|
||||
|
||||
private var TrackPayer: MediaPlayer? = null
|
||||
private var demoPath: File? = Environment .getExternalStorageDirectory()
|
||||
private var allSongs: MutableList<String>? = null
|
||||
private var currentSong: Int? = null
|
||||
private var playerInitialized: Boolean? = null
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.example.mear.management
|
||||
|
||||
import android.os.Environment
|
||||
import java.io.File
|
||||
import java.lang.Exception
|
||||
import kotlin.io.*
|
||||
|
||||
class MusicFiles (val demoPath: File) {
|
||||
|
||||
|
||||
fun loadAllMusicPaths() {
|
||||
try {
|
||||
allSongs = mutableListOf()
|
||||
val demoPath = this.demoPath.absoluteFile.toString() +"/music/"
|
||||
val f = File(demoPath)
|
||||
var count = 0
|
||||
|
||||
val folder = f
|
||||
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++
|
||||
}
|
||||
else {
|
||||
println("Stranger")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
songCount = count
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.mear.models
|
||||
|
||||
class Track (val id: Int, val title: String, val artist: String, val album: String,
|
||||
val TrackCover: ByteArray) {
|
||||
}
|
||||
Reference in New Issue
Block a user