Android Kotlin: How to programmatically play device default ringtone

Introduction

This code demonstrates two ways to play the device's default ringtone programmatically in an Android app written in Kotlin. It provides a user interface with two buttons:

  1. Play Default Ringtone: This button utilizes the Ringtone class to play the system's default ringtone.
  2. Play Ringtone As Media: This button plays the ringtone through a MediaPlayer instance, treating it as a media resource.

Breakdown of the Code

The code consists of two parts: the MainActivity.kt file containing the activity logic and the activity_main.xml file defining the user interface.

MainActivity.kt:

  1. Imports: Necessary libraries for audio playback, context, and UI components are imported.
  2. Class Definition: The MainActivity class inherits from AppCompatActivity and handles the activity lifecycle.
  3. onCreate: This method is called when the activity is first created. Here:
    • The layout (activity_main.xml) is inflated.
    • Two extension properties are defined:
      • defaultRingtone: Retrieves the system's default ringtone as a Ringtone object.
      • defaultRingtonePlayer: Creates a MediaPlayer instance configured with the default ringtone URI.
    • Button click listeners are set:
      • Button 1 (button) plays the ringtone using the defaultRingtone property and stops it on a long press.
      • Button 2 (button2) plays the ringtone using the defaultRingtonePlayer and stops it on a long press.

Extension Properties:

  • defaultRingtone:
    • Gets the default ringtone URI using RingtoneManager.
    • Creates a Ringtone object with the obtained URI.
  • defaultRingtonePlayer:
    • Retrieves the default ringtone URI from Settings.System.
    • Creates a MediaPlayer instance with the URI, effectively treating the ringtone as media.

activity_main.xml:

  • Defines the activity layout with two buttons:
    • Button 1: Triggers playing the ringtone using the Ringtone class.
    • Button 2: Triggers playing the ringtone using the MediaPlayer instance.

Summary

This code provides a flexible approach for playing the device's default ringtone. It demonstrates using both the Ringtone class for standard ringtone playing and the MediaPlayer class for more granular control over playback. The extension properties offer a convenient way to access the default ringtone throughout the activity.


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>
More android kotlin tutorials