我正在尝试播放S​​Dcard的音乐

时间:2017-06-27 13:27:27

标签: android media-player kotlin

//我正在尝试从SDCARD播放音乐,一个接一个,但我想在编辑第一首音乐后播放第二首音乐。它应该自动发生。我是新手android,任何帮助将不胜感激。与给代码。我可以播放音乐

class Play : AppCompatActivity() {


    var play: ImageView? = null
    var stop: ImageView? = null
    var AudioSavePathInDevice = "Blesson"
    var mediaPlayer: MediaPlayer? = null




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_play)

         play = findViewById(R.id.play) as ImageView
         stop = findViewById(R.id.stop) as ImageView

        play!!.setOnClickListener {

            play!!.visibility = View.INVISIBLE
            stop!!.visibility = View.VISIBLE

            val file = intent.extras["file"]
            for (hello in 1..3) {

                AudioSavePathInDevice = File("$file/bless$hello.mp3").toString()


            Toast.makeText(this, AudioSavePathInDevice, Toast.LENGTH_LONG).show()

                mediaPlayer = MediaPlayer()
                try {
                    mediaPlayer!!.setDataSource(AudioSavePathInDevice)
                    mediaPlayer!!.prepare()

                } catch (e: IOException) {
                    Toast.makeText(this, "Recoring not found",
                            Toast.LENGTH_LONG).show()
                }

                mediaPlayer!!.start()

                Toast.makeText(this, "Recording Playing",
                        Toast.LENGTH_LONG).show()

                mediaPlayer!!.setOnCompletionListener{}

                try {
                    mediaPlayer!!.stop()
                    mediaPlayer!!.release()
                }catch (ex: Exception){ }
            }


        }
    }

}

0 个答案:

没有答案