FileDocCategorySizeDatePackage
Ringtone.javaAPI DocAndroid 1.5 API7253Wed May 06 22:42:00 BST 2009android.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 String
TAG
private static final String[]
MEDIA_COLUMNS
private static final String[]
DRM_COLUMNS
private android.media.MediaPlayer
mAudio
private android.net.Uri
mUri
private String
mTitle
private FileDescriptor
mFileDescriptor
private android.content.res.AssetFileDescriptor
mAssetFileDescriptor
private int
mStreamType
private android.content.Context
mContext
Constructors Summary
Ringtone(android.content.Context context)


      
        mContext = context;
    
Methods Summary
public intgetStreamType()
Gets the stream type where this ringtone will be played.

return
The stream type, see {@link AudioManager}.

        return mStreamType;
    
public java.lang.StringgetTitle(android.content.Context context)
Returns a human-presentable title for ringtone. Looks in media and DRM content providers. 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 {
                
                if (DrmStore.AUTHORITY.equals(authority)) {
                    cursor = res.query(uri, DRM_COLUMNS, null, null, null);
                } else if (MediaStore.AUTHORITY.equals(authority)) {
                    cursor = res.query(uri, MEDIA_COLUMNS, null, null, null);
                }
                
                if (cursor != null && cursor.getCount() == 1) {
                    cursor.moveToFirst();
                    return cursor.getString(2);
                } else {
                    title = uri.getLastPathSegment();
                }
            }
        }

        if (title == null) {
            title = context.getString(com.android.internal.R.string.ringtone_unknown);
            
            if (title == null) {
                title = "";
            }
        }
        
        return title;
    
public booleanisPlaying()
Whether this ringtone is currently playing.

return
True if playing, false otherwise.

        return mAudio != null && mAudio.isPlaying();
    
voidopen(java.io.FileDescriptor fd)

        mFileDescriptor = fd;
        openMediaPlayer();
    
voidopen(android.content.res.AssetFileDescriptor fd)

        mAssetFileDescriptor = fd;
        openMediaPlayer();
    
voidopen(android.net.Uri uri)

        mUri = uri;
        openMediaPlayer();
    
private voidopenMediaPlayer()

        mAudio = new MediaPlayer();
        if (mUri != null) {
            mAudio.setDataSource(mContext, mUri);
        } else if (mFileDescriptor != null) {
            mAudio.setDataSource(mFileDescriptor);
        } else if (mAssetFileDescriptor != null) {
            // Note: using getDeclaredLength so that our behavior is the same
            // as previous versions when the content provider is returning
            // a full file.
            if (mAssetFileDescriptor.getDeclaredLength() < 0) {
                mAudio.setDataSource(mAssetFileDescriptor.getFileDescriptor());
            } else {
                mAudio.setDataSource(mAssetFileDescriptor.getFileDescriptor(),
                        mAssetFileDescriptor.getStartOffset(),
                        mAssetFileDescriptor.getDeclaredLength());
            }
        } else {
            throw new IOException("No data source set.");
        }
        mAudio.setAudioStreamType(mStreamType);
        mAudio.prepare();
    
public voidplay()
Plays the ringtone.

        if (mAudio == null) {
            try {
                openMediaPlayer();
            } catch (Exception ex) {
                Log.e(TAG, "play() caught ", ex);
                mAudio = null;
            }
        }
        if (mAudio != null) {
            mAudio.start();
        }
    
public voidsetStreamType(int streamType)
Sets the stream type where this ringtone will be played.

param
streamType The stream, see {@link AudioManager}.

        mStreamType = streamType;
        
        if (mAudio != null) {
            /*
             * The stream type has to be set before the media player is
             * prepared. Re-initialize it.
             */
            try {
                openMediaPlayer();
            } catch (IOException e) {
                Log.w(TAG, "Couldn't set the stream type", e);
            }
        }
    
voidsetTitle(java.lang.String title)

        mTitle = title;
    
public voidstop()
Stops a playing ringtone.

        if (mAudio != null) {
            mAudio.reset();
            mAudio.release();
            mAudio = null;
        }