FileDocCategorySizeDatePackage
Ringtone.javaAPI DocAndroid 5.1 API12235Thu Mar 12 22:22:30 GMT 2015android.media

Ringtone

public class Ringtone extends Object
Ringtone provides a quick method for playing a ringtone, notification, or other similar types of sounds.

For ways of retrieving {@link Ringtone} objects or to show a ringtone picker, see {@link RingtoneManager}.

see
RingtoneManager

Fields Summary
private static final String
TAG
private static final boolean
LOGD
private static final String[]
MEDIA_COLUMNS
private final android.content.Context
mContext
private final AudioManager
mAudioManager
private final boolean
mAllowRemote
Flag indicating if we're allowed to fall back to remote playback using {@link #mRemotePlayer}. Typically this is false when we're the remote player and there is nobody else to delegate to.
private final IRingtonePlayer
mRemotePlayer
private final android.os.Binder
mRemoteToken
private MediaPlayer
mLocalPlayer
private android.net.Uri
mUri
private String
mTitle
private AudioAttributes
mAudioAttributes
Constructors Summary
public Ringtone(android.content.Context context, boolean allowRemote)
{@hide}


      
         
        mContext = context;
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        mAllowRemote = allowRemote;
        mRemotePlayer = allowRemote ? mAudioManager.getRingtonePlayer() : null;
        mRemoteToken = allowRemote ? new Binder() : null;
    
Methods Summary
private voiddestroyLocalPlayer()

        if (mLocalPlayer != null) {
            mLocalPlayer.reset();
            mLocalPlayer.release();
            mLocalPlayer = null;
        }
    
public AudioAttributesgetAudioAttributes()
Returns the {@link AudioAttributes} used by this object.

return
the {@link AudioAttributes} that were set with {@link #setAudioAttributes(AudioAttributes)} or the default attributes if none were set.

        return mAudioAttributes;
    
public intgetStreamType()
Gets the stream type where this ringtone will be played.

return
The stream type, see {@link AudioManager}.
deprecated
use of stream types is deprecated, see {@link #setAudioAttributes(AudioAttributes)}

        return AudioAttributes.toLegacyStreamType(mAudioAttributes);
    
public java.lang.StringgetTitle(android.content.Context context)
Returns a human-presentable title for ringtone. Looks in media content provider. If not in either, uses the filename

param
context A context used for querying.

        if (mTitle != null) return mTitle;
        return mTitle = getTitle(context, mUri, true);
    
private static java.lang.StringgetTitle(android.content.Context context, android.net.Uri uri, boolean followSettingsUri)

        Cursor cursor = null;
        ContentResolver res = context.getContentResolver();
        
        String title = null;

        if (uri != null) {
            String authority = uri.getAuthority();

            if (Settings.AUTHORITY.equals(authority)) {
                if (followSettingsUri) {
                    Uri actualUri = RingtoneManager.getActualDefaultRingtoneUri(context,
                            RingtoneManager.getDefaultType(uri));
                    String actualTitle = getTitle(context, actualUri, false);
                    title = context
                            .getString(com.android.internal.R.string.ringtone_default_with_actual,
                                    actualTitle);
                }
            } else {
                try {
                    if (MediaStore.AUTHORITY.equals(authority)) {
                        cursor = res.query(uri, MEDIA_COLUMNS, null, null, null);
                    }
                } catch (SecurityException e) {
                    // missing cursor is handled below
                }

                try {
                    if (cursor != null && cursor.getCount() == 1) {
                        cursor.moveToFirst();
                        return cursor.getString(2);
                    } else {
                        title = uri.getLastPathSegment();
                    }
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
            }
        }

        if (title == null) {
            title = context.getString(com.android.internal.R.string.ringtone_unknown);
            
            if (title == null) {
                title = "";
            }
        }
        
        return title;
    
public android.net.UrigetUri()
{@hide}

        return mUri;
    
public booleanisPlaying()
Whether this ringtone is currently playing.

return
True if playing, false otherwise.

        if (mLocalPlayer != null) {
            return mLocalPlayer.isPlaying();
        } else if (mAllowRemote && (mRemotePlayer != null)) {
            try {
                return mRemotePlayer.isPlaying(mRemoteToken);
            } catch (RemoteException e) {
                Log.w(TAG, "Problem checking ringtone: " + e);
                return false;
            }
        } else {
            Log.w(TAG, "Neither local nor remote playback available");
            return false;
        }
    
public voidplay()
Plays the ringtone.

        if (mLocalPlayer != null) {
            // do not play ringtones if stream volume is 0
            // (typically because ringer mode is silent).
            if (mAudioManager.getStreamVolume(
                    AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) {
                mLocalPlayer.start();
            }
        } else if (mAllowRemote && (mRemotePlayer != null)) {
            final Uri canonicalUri = mUri.getCanonicalUri();
            try {
                mRemotePlayer.play(mRemoteToken, canonicalUri, mAudioAttributes);
            } catch (RemoteException e) {
                if (!playFallbackRingtone()) {
                    Log.w(TAG, "Problem playing ringtone: " + e);
                }
            }
        } else {
            if (!playFallbackRingtone()) {
                Log.w(TAG, "Neither local nor remote playback available");
            }
        }
    
private booleanplayFallbackRingtone()

        if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes))
                != 0) {
            int ringtoneType = RingtoneManager.getDefaultType(mUri);
            if (ringtoneType == -1 ||
                    RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType) != null) {
                // Default ringtone, try fallback ringtone.
                try {
                    AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(
                            com.android.internal.R.raw.fallbackring);
                    if (afd != null) {
                        mLocalPlayer = new MediaPlayer();
                        if (afd.getDeclaredLength() < 0) {
                            mLocalPlayer.setDataSource(afd.getFileDescriptor());
                        } else {
                            mLocalPlayer.setDataSource(afd.getFileDescriptor(),
                                    afd.getStartOffset(),
                                    afd.getDeclaredLength());
                        }
                        mLocalPlayer.setAudioAttributes(mAudioAttributes);
                        mLocalPlayer.prepare();
                        mLocalPlayer.start();
                        afd.close();
                        return true;
                    } else {
                        Log.e(TAG, "Could not load fallback ringtone");
                    }
                } catch (IOException ioe) {
                    destroyLocalPlayer();
                    Log.e(TAG, "Failed to open fallback ringtone");
                } catch (NotFoundException nfe) {
                    Log.e(TAG, "Fallback ringtone does not exist");
                }
            } else {
                Log.w(TAG, "not playing fallback for " + mUri);
            }
        }
        return false;
    
public voidsetAudioAttributes(AudioAttributes attributes)
Sets the {@link AudioAttributes} for this ringtone.

param
attributes the non-null attributes characterizing this ringtone.

        if (attributes == null) {
            throw new IllegalArgumentException("Invalid null AudioAttributes for Ringtone");
        }
        mAudioAttributes = attributes;
        // The audio attributes have to be set before the media player is prepared.
        // Re-initialize it.
        setUri(mUri);
    
public voidsetStreamType(int streamType)
Sets the stream type where this ringtone will be played.

param
streamType The stream, see {@link AudioManager}.
deprecated
use {@link #setAudioAttributes(AudioAttributes)}

        setAudioAttributes(new AudioAttributes.Builder()
                .setInternalLegacyStreamType(streamType)
                .build());
    
voidsetTitle(java.lang.String title)

        mTitle = title;
    
public voidsetUri(android.net.Uri uri)
Set {@link Uri} to be used for ringtone playback. Attempts to open locally, otherwise will delegate playback to remote {@link IRingtonePlayer}.

hide

        destroyLocalPlayer();

        mUri = uri;
        if (mUri == null) {
            return;
        }

        // TODO: detect READ_EXTERNAL and specific content provider case, instead of relying on throwing

        // try opening uri locally before delegating to remote player
        mLocalPlayer = new MediaPlayer();
        try {
            mLocalPlayer.setDataSource(mContext, mUri);
            mLocalPlayer.setAudioAttributes(mAudioAttributes);
            mLocalPlayer.prepare();

        } catch (SecurityException | IOException e) {
            destroyLocalPlayer();
            if (!mAllowRemote) {
                Log.w(TAG, "Remote playback not allowed: " + e);
            }
        }

        if (LOGD) {
            if (mLocalPlayer != null) {
                Log.d(TAG, "Successfully created local player");
            } else {
                Log.d(TAG, "Problem opening; delegating to remote player");
            }
        }
    
public voidstop()
Stops a playing ringtone.

        if (mLocalPlayer != null) {
            destroyLocalPlayer();
        } else if (mAllowRemote && (mRemotePlayer != null)) {
            try {
                mRemotePlayer.stop(mRemoteToken);
            } catch (RemoteException e) {
                Log.w(TAG, "Problem stopping ringtone: " + e);
            }
        }