Is there a way to use Media Player in Kotlin?

55 Views Asked by At

control audio playback. Below is a basic example of how you might implement a media player with start, pause, forward, and rewind functionalities. This example assumes you have an audio file named audio_sample.mp3 in your res/raw directory.

        private val FORWARD_REWIND_INTERVAL = 5000 // milliseconds
        mediaPlayer = MediaPlayer.create(this, R.raw.blame)

        findViewById<Button>(R.id.btnPlay).setOnClickListener {
            if (!mediaPlayer.isPlaying) {
                mediaPlayer.start()
            }
        }

        findViewById<Button>(R.id.btnPause).setOnClickListener {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.pause()
            }
        }

        findViewById<Button>(R.id.btnForward).setOnClickListener {
            val currentPosition = mediaPlayer.currentPosition
            val duration = mediaPlayer.duration
            if (currentPosition + FORWARD_REWIND_INTERVAL < duration) {
                mediaPlayer.seekTo(currentPosition + FORWARD_REWIND_INTERVAL)
            } else {
                mediaPlayer.seekTo(duration)
            }
        }

        findViewById<Button>(R.id.btnRewind).setOnClickListener {
            val currentPosition = mediaPlayer.currentPosition
            if (currentPosition - FORWARD_REWIND_INTERVAL > 0) {
                mediaPlayer.seekTo(currentPosition - FORWARD_REWIND_INTERVAL)
            } else {
                mediaPlayer.seekTo(0)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mediaPlayer.isPlaying) {
            mediaPlayer.stop()
        }
        mediaPlayer.release()
    }

I having error in this code ,what could be the problem Developing a simple media player application in Android involves using the MediaPlayer class to

2

There are 2 best solutions below

1
ROXY On

THis how you can do it

val countries= arrayOf("Select Country", "USA", "Canada", "India", "Australia")
    spin.adapter=ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,countries)

    submit.setOnClickListener {
        val radioID=sex_rg.checkedRadioButtonId
        val rb=findViewById<RadioButton>(radioID)
        val gender=rb.text.toString()
        val name1=name.text.toString()
        val terms=chkbox.isChecked
        val coun=spin.selectedItem.toString()

        val intent=Intent(this,Result::class.java).apply {
            putExtra("name",name1)
            putExtra("gender",gender)
            putExtra("terms",terms)
            putExtra("country",coun)
    }
        startActivity(intent)
val name=intent.getStringExtra("name")
    val gender = intent.getStringExtra("gender")
    val termsAccepted = intent.getBooleanExtra("termsAccepted", false)
    val country = intent.getStringExtra("country")
1
John Doe On
val submitButton = findViewById<Button>(R.id.submit_button)
        submitButton.setOnClickListener {
            showAlertDialog()
        }

private fun showAlertDialog() {
        AlertDialog.Builder(this)
            .setTitle("Submit Confirmation")
            .setMessage("Are you sure you want to submit?")
            .setPositiveButton("Yes") { dialog, which ->
                dialog.dismiss()
                Toast.makeText(this, "Submission Confirmed", Toast.LENGTH_SHORT).show()
                calcres()

            }
            .setNegativeButton("No") { dialog, which ->
                dialog.dismiss()
            }
            .show()
    }

    private fun calcres() {
        val radioGroupIds = arrayOf(
            R.id.RG1, R.id.RG2, R.id.RG3, R.id.RG4, R.id.RG5,
            R.id.RG6, R.id.RG7, R.id.RG8, R.id.RG9, R.id.RG10,
            R.id.RG11, R.id.RG12 // Add IDs for the new RadioGroups
        )

        val correctans = mutableListOf(
            "True", "False", "True", "False", "True",
            "False", "True", "False", "True", "False",
            "True", "False" // Add the correct answers for the new questions
        )
        val urans = mutableListOf<String>()
        var score = 0

        radioGroupIds.forEachIndexed { index, radioGroupId ->
            val radioGroup = findViewById<RadioGroup>(radioGroupId)
            val selectedRadioButtonId = radioGroup.checkedRadioButtonId
            if (selectedRadioButtonId != -1) {
                val radioButton = findViewById<RadioButton>(selectedRadioButtonId)
                val selectedText = radioButton.text.toString()
                urans.add(selectedText)
                if (selectedText == correctans[index]) {
                    score++
                }
            } else {
                urans.add("Not Answered")
            }
        }

        tempScore = score
        checkPermissionsAndShowNotification(score)
        val intent = Intent(this, MainActivity2::class.java).apply {
            putExtra("score", score)
            putStringArrayListExtra("ans", ArrayList(urans))
        }
        startActivity(intent)
    }

#ON NEXT PAGE 
val getans:ArrayList<String>?=intent.getStringArrayListExtra("ans")
        val answersText = getans?.joinToString(separator = "\n") ?: "No answers provided"
        val score=intent.getIntExtra("score",0)