package com.example.music

import android.media.MediaPlayer
import android.media.PlaybackParams
import android.os.Bundle
import androidx.activity.ComponentActivity
import java.util.concurrent.TimeUnit


class MainActivity : ComponentActivity() {

    override fun onCreate (savedInstanceState:Bundle?) {

        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
    }

    override fun onResume () {
        super.onResume()
        try {
            val mp = MediaPlayer()
            mp.setDataSource ("http://philos.nmu.edu/polka/polka.mp3")
            mp.prepare()
            val params: PlaybackParams = mp.getPlaybackParams()
            params.setSpeed(0.5f)
            mp.setPlaybackParams(params)
            mp.start()  //It plays in the background, the app is still running.
            try {
                TimeUnit.SECONDS.sleep (10)  //I sleep for ten seconds and then throw an exception
            } catch (e:Exception) {
            }
            mp.pause ()
            try {
                TimeUnit.SECONDS.sleep (5)
            } catch (e:Exception) {
            }
            mp.start ()
            try {
                TimeUnit.SECONDS.sleep (5)
            } catch (e:Exception) {
            }
            mp.reset()
            mp.setDataSource ("http://philos.nmu.edu/polka/polka.mp3")
            mp.prepare()
            mp.start()


        } catch (e:Exception) {
            e.printStackTrace()
        }

    }
}

