How to programmatically play phone default ring tone with Android

In this android programming source code example, we are going to play phone default ring tone with Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to play phone default ring tone with Android.

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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DeviceFeatures.DeviceFeaturesActivity20">

    <Button
        android:id="@+id/playRingtone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Play Ringtone"
        android:textColor="#ffffff"
        android:background="@color/colorAccent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <Button
        android:id="@+id/stopRingtone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop Ringtone"
        android:textColor="#ffffff"
        android:background="@color/colorAccent"
        android:layout_marginTop="110dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;

import com.bluapp.androidview2.R;

public class DeviceFeaturesActivity19 extends AppCompatActivity {
    private Button playRingtone;
    private Button stopRingtone;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_features19);
        playRingtone = (Button) findViewById(R.id.playRingtone);
        stopRingtone = (Button) findViewById(R.id.stopRingtone);
        try {
        mediaPlayer = MediaPlayer.create(DeviceFeaturesActivity19.this, Settings.System.DEFAULT_RINGTONE_URI);
        mediaPlayer.prepare();
        mediaPlayer.setVolume(1f, 1f);
        mediaPlayer.setLooping(false);
        }catch (Exception e){
            e.printStackTrace();
        }

        playRingtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.start();
            }
        });

        stopRingtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                }
            }
        });

    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment