Saturday, April 21, 2018

android kotlin - Play default ringtone example

MainActivity.kt

package com.cfsuman.kotlinexamples

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


class MainActivity : AppCompatActivity() {

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

        // Get the device default ringtone
        val ringtone:Ringtone = defaultRingtone

        // Display the default ringtone title
        text_view.text = "Ringtone : ${ringtone.getTitle(applicationContext)}"

        // Play the default ringtone
        button_play.setOnClickListener{
            if (!ringtone.isPlaying){
                ringtone.play()
            }
        }

        // Stop playing ringtone
        button_stop.setOnClickListener{
            if (ringtone.isPlaying){
                ringtone.stop()
            }
        }


        // Get the media player for default ringtone
        val mediaPlayer:MediaPlayer = defaultRingtonePlayer()

        // Play default ringtone using media player
        button_media_play.setOnClickListener{
            if (!mediaPlayer.isPlaying)mediaPlayer.start()
        }

        // Stop media player
        button_media_stop.setOnClickListener{
            if (mediaPlayer.isPlaying){
                mediaPlayer.stop()
            }
        }
    }
}



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


// Extension function to get a media player to play default ringtone
fun Context.defaultRingtonePlayer():MediaPlayer{
    return MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI)
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_layout"
    android:background="#e7faef"
    android:padding="16dp"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff117c"
        style="@style/Base.TextAppearance.AppCompat.Large"
        />
    <Button
        android:id="@+id/button_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play Default Ringtone"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/button_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/button_media_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play Using Media Player"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/button_media_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop Media Player"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        />
</LinearLayout>