在Kotlin中扫描设备中的文件并对其排序.mp3文件

时间:2020-09-15 12:01:41

标签: android kotlin local-storage audio-player

我正在尝试在Kotlin中创建一个音乐应用程序。目前,我已经设法播放和暂停预定义的歌曲,并且已经将搜索栏集成到其中。但是,我想播放设备本地存储中的歌曲。因此,我想将歌曲存储在手机中。我在堆栈溢出中找到了几种解决方案,但是,所有解决方案都使用Java。谁能帮我解决这个问题?

我当前完成的代码是

package com.ashiquemusicplayer.mp3player

import android.annotation.SuppressLint
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.widget.SeekBar
import kotlinx.android.synthetic.main.activity_current_playing.*

class CurrentPlayingActivity : AppCompatActivity() {

    private lateinit var mp: MediaPlayer
    private var totalTime = 0

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_current_playing)

        // Play and pause song
        playPauseButton.setOnClickListener {
            playBtnClick()
        }

        // Assigning the music for playing
        mp = MediaPlayer.create(this, R.raw.closer)
        mp.isLooping = true

        // Progressbar creating
        totalTime = mp.duration
        progressBar.max = totalTime
        progressBar.setOnSeekBarChangeListener(
            object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                    if (fromUser) {
                        mp.seekTo(progress)
                    }
                }
                override fun onStartTrackingTouch(p0: SeekBar?) {
                }
                override fun onStopTrackingTouch(p0: SeekBar?) {
                }
            }
        )

        // Threading progressbar for user to change the progressbar and for moving the music to their desired time of the music
        Thread {
            while (mp != null) {
                val msg = Message()
                msg.what = mp.currentPosition
                handler.sendMessage(msg)
                Thread.sleep(1)
            }
        }.start()
    }

    @SuppressLint("HandlerLeak")
    var handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            val currentPosition = msg.what
            progressBar.progress = currentPosition
            val elapsedTime = createTimeLabel(currentPosition)
            elapsedTimeLabel.text = elapsedTime
            val remainingTime = createTimeLabel(totalTime - currentPosition)
            remainingTimeLabel.text = "-$remainingTime"
        }
    }

    // Making the time label of current position of the song
    fun createTimeLabel(time: Int): String {
        var timeLabel: String
        val min = time / 1000 / 60
        val sec = time / 1000 % 60
        timeLabel = "$min:"
        if (sec < 10) {
            timeLabel += "0"
        }
        timeLabel += sec
        return timeLabel
    }

    // playing and pausing song and changing the button image
    private fun playBtnClick() {
        if (mp.isPlaying) {
            mp.pause()
            playPauseButton.setBackgroundResource(R.drawable.play)
        } else {
            mp.start()
            playPauseButton.setBackgroundResource(R.drawable.pause)
        }
    }
}

我已使用以下代码获取文件。但是,即使目录中有文件,也表明目录中没有文件。

private fun getSongs() {
        val fileList: ArrayList<HashMap<String, String>> = ArrayList()
        val rootFolder: File = File(Environment.DIRECTORY_MUSIC)
        val files =
            rootFolder.listFiles()
        if (files == null) {
            Log.d("result", "empty")
        } else {
            for (file in files) {
                Log.d("result", "not empty")
                if (file.name.endsWith(".mp3")) {
                    val song: HashMap<String, String> = HashMap()
                    song["file_path"] = file.absolutePath
                    song["file_name"] = file.name
                    fileList.add(song)
                    Log.d("resulted file", file.name)
                }
            }
        }
    }

0 个答案:

没有答案