android - Play default ringtone programmatically

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout     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"     android:background="#99a19c"     android:padding="16dp"     android:orientation="vertical"     >     <Button         android:id="@+id/btn_play_ringtone"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Play Default Ringtone"         />     <Button         android:id="@+id/btn_play_as_media"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Play Ringtone As Media"         /> </LinearLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.app.Activity; import android.content.Context; import android.media.MediaPlayer; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.provider.Settings; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button;   public class MainActivity extends AppCompatActivity {     private Context mContext;     private Activity mActivity;      private Button mButtonRingtone;     private Button mButtonMedia;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the application context         mContext = getApplicationContext();         mActivity = MainActivity.this;          // Get the widget reference from xml layout         mButtonRingtone = findViewById(R.id.btn_play_ringtone);         mButtonMedia = findViewById(R.id.btn_play_as_media);          /*             RingtoneManager                 RingtoneManager provides access to ringtones, notification, and other types of sounds.                 It manages querying the different media providers and combines the results into                 a single cursor. It also provides a Ringtone for each ringtone. We generically                 call these sounds ringtones, however the TYPE_RINGTONE refers to the type                 of sounds that are suitable for the phone ringer.         */          /*             Uri getDefaultUri (int type)                 Returns the Uri for the default ringtone of a particular type. Rather than returning                 the actual ringtone's sound Uri, this will return the symbolic Uri which                 will resolved to the actual sound when played.              Parameters                 type : int: The ringtone type whose default should be returned.              Returns                 Uri : The Uri of the default ringtone for the given type.         */          /*             Ringtone getRingtone (Context context, Uri ringtoneUri)                 Returns a Ringtone for a given sound URI.                  If the given URI cannot be opened for any reason, this method will attempt                 to fallback on another sound. If it cannot find any, it will return null.              Parameters                 context Context : A context used to query.                 ringtoneUri Uri : The Uri of a sound or ringtone.             Returns                 Ringtone : A Ringtone for the given URI, or null.         */          // Play the default ringtone as ringtone         mButtonRingtone.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);                 Ringtone ringtone = RingtoneManager.getRingtone(mContext,uri);                 ringtone.play();             }         });          /*             MediaPlayer                 MediaPlayer class can be used to control playback of audio/video files and streams.         */          /*             MediaPlayer create (Context context, Uri uri)                 Convenience method to create a MediaPlayer for a given Uri. On success, prepare()                 will already have been called and must not be called again.                  When done with the MediaPlayer, you should call release(), to free the resources.                 If not released, too many MediaPlayer instances will result in an exception.                  Note that since prepare() is called automatically in this method, you cannot change                 the audio session ID (see setAudioSessionId(int)) or audio attributes                 (see setAudioAttributes(AudioAttributes) of the new MediaPlayer.              Parameters                 context Context : the Context to use                 uri Uri : the Uri from which to get the datasource             Returns                 MediaPlayer : a MediaPlayer object, or null if creation failed         */          // Play the device default ringtone using media player         mButtonMedia.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 MediaPlayer player = MediaPlayer.create(mActivity, Settings.System.DEFAULT_RINGTONE_URI);                 player.start();             }         });     } } 

Komentar