예제 #1
0
 /** Stops a playing ringtone. */
 public void stop() {
   if (mLocalPlayer != null) {
     destroyLocalPlayer();
   } else if (mAllowRemote && (mRemotePlayer != null)) {
     try {
       mRemotePlayer.stop(mRemoteToken);
     } catch (RemoteException e) {
       Log.w(TAG, "Problem stopping ringtone: " + e);
     }
   }
 }
예제 #2
0
  /**
   * Set {@link Uri} to be used for ringtone playback. Attempts to open locally, otherwise will
   * delegate playback to remote {@link IRingtonePlayer}.
   *
   * @hide
   */
  public void setUri(Uri uri) {
    destroyLocalPlayer();

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

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

    if (isSoundCustomized()) {
      // instead of restore to default ringtone.
      restoreRingtoneIfNotExist(Settings.System.RINGTONE);
      restoreRingtoneIfNotExist(Settings.System.RINGTONE_2);
    }

    // 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");
      }
    }
  }
예제 #3
0
 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;
 }