/** Plays the ringtone. */ public void play() { 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 boolean playFallbackRingtone() { if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) { int subId = RingtoneManager.getDefaultRingtoneSubIdByUri(mUri); if (subId != -1 && RingtoneManager.getActualRingtoneUriBySubId(mContext, subId) != 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; }
/** * 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)} */ @Deprecated public int getStreamType() { return AudioAttributes.toLegacyStreamType(mAudioAttributes); }