Thursday, August 13, 2020

android kotlin - Play default ringtone programmatically

MainActivity.kt

package com.example.jetpack

import android.content.Context
import android.media.MediaPlayer
import android.media.Ringtone
import android.media.RingtoneManager
import android.os.Bundle
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        // get device default ringtone and play
        val ringtone = applicationContext.defaultRingtone
        button.setOnClickListener {
            ringtone.play()
        }

        // stop playing ringtone
        button.setOnLongClickListener {
            ringtone.stop()
            true
        }


        // play default ringtone as media
        val player  = applicationContext.defaultRingtonePlayer
        button2.setOnClickListener {
            player.start()
        }

        // stop media player
        button2.setOnLongClickListener {
            player.stop()
            true
        }
    }
}


// extension property to get device default ringtone
val Context.defaultRingtone:Ringtone
    get() {
        val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
        return RingtoneManager.getRingtone(this,uri)
    }


// extension property to get media player with default ringtone
val Context.defaultRingtonePlayer:MediaPlayer
    get() {
        // get default ringtone uri
        val uri = Settings.System.DEFAULT_RINGTONE_URI
        // create media player with default ringtone
        return MediaPlayer.create(this, uri)
    }
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E5E4E2"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="Play Default Ringtone"
        android:backgroundTint="#00563F"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Play Ringtone As Media"
        android:backgroundTint="#7B3F00"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>