2일차...
리스트를 누르면 음악재생 음악제목 앨범사진이 클릭한 음악에 맞게
출력되길 원했는데...
왠지모르지만 되긴함..왜 되는건지 이유를 모를뿐..
버튼으로 다음곡으로 넘어가기 도전했지만,,,실패
package com.example.recommend_music
import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.os.SystemClock
import android.view.View
import android.widget.*
import androidx.core.app.ActivityCompat
import java.io.File
import java.text.SimpleDateFormat
class Music_play : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_music_play)
var musicImg = findViewById<ImageView>(R.id.musicImag)
var musictitle = findViewById<TextView>(R.id.musicTitle)
var time1 = findViewById<TextView>(R.id.time1)
var bar1 = findViewById<SeekBar>(R.id.bar1)
var list = findViewById<ListView>(R.id.list1)
// var btn1 = findViewById<Button>(R.id.btn4)
// var btn2 = findViewById<ImageButton>(R.id.btn2)
// var btn3 = findViewById<ImageButton>(R.id.btn3)
var mp3List : ArrayList<String>
var mp3Path = "/sdcard/"
var selectmp3 :String
var Imags = arrayOf(R.drawable.m_1,R.drawable.m_2,R.drawable.m_3)
musicImg.setImageResource(Imags[0])
mp3List = ArrayList()
var listFiles = File(mp3Path).listFiles()
var fileName:String
var extName:String
for(file in listFiles!!){
fileName = file.name //파일이름
if (!file.isDirectory){
extName = fileName.substring(fileName.length-3)
if (extName =="mp3"){
mp3List.add(fileName)
}
}
}
var index = mp3List.size
var adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,mp3List)
list.adapter = adapter
var mPlayer = MediaPlayer()
list.setOnItemClickListener { parent, view, position, id ->
musicImg.setImageResource(Imags[position])
selectmp3 = mp3List[position]
musictitle.text = "$selectmp3"
mPlayer.isLooping=true
if (mPlayer.isPlaying == false){
mPlayer.setDataSource(mp3Path + selectmp3) //음악 경로임
mPlayer.prepare()
mPlayer.start()
}
else{
mPlayer.stop()
time1.text = "재생시간 :"
mPlayer.reset()
}
object :Thread(){
var timeFormat = SimpleDateFormat("mm:ss")
override fun run() {
super.run()
if (mPlayer ==null){
return
}
bar1.max = mPlayer.duration
while (mPlayer.isPlaying){
runOnUiThread {
bar1.progress =mPlayer.currentPosition
time1.text = "재생시간 : "+timeFormat.format(mPlayer.currentPosition)
}
SystemClock.sleep(200)
}
}
}.start()
}
//음악 멈춤 reset을해줘야 리스트를 다시 누를때 음악나옴....아님 터짐 이거때문에......별거아닌건데 왜 한참찾은거지
// btn2.setOnClickListener {
// mPlayer.stop()
// mPlayer.reset()
// }
}
}