Java Code Examples for android.media.Ringtone#play()

The following examples show how to use android.media.Ringtone#play() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Notification.java    From cordova-android-chromeview with Apache License 2.0 6 votes vote down vote up
/**
 * Beep plays the default notification ringtone.
 *
 * @param count     Number of times to play notification
 */
public void beep(long count) {
    Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone notification = RingtoneManager.getRingtone(this.cordova.getActivity().getBaseContext(), ringtone);

    // If phone is not set to silent mode
    if (notification != null) {
        for (long i = 0; i < count; ++i) {
            notification.play();
            long timeout = 5000;
            while (notification.isPlaying() && (timeout > 0)) {
                timeout = timeout - 100;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
        }
    }
}
 
Example 2
Source File: ProgressBarController.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
private void maybePlaySound() {
    if (mSoundEnabled &&
            (!mPowerManager.isInteractive() || !mSoundWhenScreenOffOnly)) {
        try {
            final Ringtone sfx = RingtoneManager.getRingtone(mContext,
                    Uri.parse(mSoundUri));
            if (sfx != null) {
                AudioAttributes attrs = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .build();
                sfx.setAudioAttributes(attrs);
                sfx.play();
            }
        } catch (Throwable t) {
            XposedBridge.log(t);
        }
    }
}
 
Example 3
Source File: Main.java    From chess with Apache License 2.0 6 votes vote down vote up
@Override
public void onTurnBasedMatchReceived(final TurnBasedMatch match) {
    if (BuildConfig.DEBUG) Logger.log("Main onTurnBasedMatchReceived: " + match.getMatchId());
    if (match.getTurnStatus() == TurnBasedMatch.MATCH_TURN_STATUS_MY_TURN &&
            match.getStatus() == TurnBasedMatch.MATCH_STATUS_ACTIVE) {
        final Ringtone tone = RingtoneManager.getRingtone(this, RingtoneManager
                .getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION));
        tone.setStreamType(AudioManager.STREAM_NOTIFICATION);
        tone.play();
    }
    if (startFragment != null && startFragment.isVisible()) {
        startFragment.loadMatches();
    }
    if (gameFragment != null && gameFragment.isVisible() &&
            match.getMatchId().equals(gameFragment.currentMatch)) {
        if (Game.load(match.getData(), new Match(match), mGoogleApiClient)) {
            gameFragment.update(match.getStatus() != TurnBasedMatch.MATCH_STATUS_ACTIVE &&
                    match.getStatus() != TurnBasedMatch.MATCH_STATUS_AUTO_MATCHING);
        } else {
            updateApp();
        }
    }
}
 
Example 4
Source File: Notification.java    From phonegapbootcampsite with MIT License 6 votes vote down vote up
/**
 * Beep plays the default notification ringtone.
 *
 * @param count     Number of times to play notification
 */
public void beep(long count) {
    Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone notification = RingtoneManager.getRingtone(this.cordova.getActivity().getBaseContext(), ringtone);

    // If phone is not set to silent mode
    if (notification != null) {
        for (long i = 0; i < count; ++i) {
            notification.play();
            long timeout = 5000;
            while (notification.isPlaying() && (timeout > 0)) {
                timeout = timeout - 100;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
        }
    }
}
 
Example 5
Source File: RingtonePickerActivity.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
public void run() {
    stopAnyPlayingRingtone();
    if (mSampleRingtonePos == mSilentPos) {
        return;
    }

    Ringtone ringtone;
    if (mSampleRingtonePos == mDefaultRingtonePos) {
        if (mDefaultRingtone == null) {
            mDefaultRingtone = RingtoneManager.getRingtone(this, mUriForDefaultItem);
        }
       /*
        * Stream type of mDefaultRingtone is not set explicitly here.
        * It should be set in accordance with mRingtoneManager of this Activity.
        */
        if (mDefaultRingtone != null) {
            mDefaultRingtone.setStreamType(mRingtoneManager.inferStreamType());
        }
        ringtone = mDefaultRingtone;
        mCurrentRingtone = null;
    } else {
        ringtone = mRingtoneManager.getRingtone(getRingtoneManagerPosition(mSampleRingtonePos));
        mCurrentRingtone = ringtone;
    }

    if (ringtone != null) {
        ringtone.play();
    }
}
 
Example 6
Source File: DefaultMessageNotifier.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static void sendInThreadNotification(Context context, Recipient recipient) {
  if (!TextSecurePreferences.isInThreadNotifications(context) ||
      ServiceUtil.getAudioManager(context).getRingerMode() != AudioManager.RINGER_MODE_NORMAL)
  {
    return;
  }

  Uri uri = null;
  if (recipient != null) {
    uri = NotificationChannels.supported() ? NotificationChannels.getMessageRingtone(context, recipient) : recipient.getMessageRingtone();
  }

  if (uri == null) {
    uri = NotificationChannels.supported() ? NotificationChannels.getMessageRingtone(context) : TextSecurePreferences.getNotificationRingtone(context);
  }

  if (uri.toString().isEmpty()) {
    Log.d(TAG, "ringtone uri is empty");
    return;
  }

  Ringtone ringtone = RingtoneManager.getRingtone(context, uri);

  if (ringtone == null) {
    Log.w(TAG, "ringtone is null");
    return;
  }

  if (Build.VERSION.SDK_INT >= 21) {
    ringtone.setAudioAttributes(new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
                                                             .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
                                                             .build());
  } else {
    ringtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
  }

  ringtone.play();
}
 
Example 7
Source File: NotificationService.java    From Ecommerce-Morningmist-Android with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
public void showNotification(String msg) {

		int stringId = getApplicationContext().getApplicationInfo().labelRes;
		String appName = getApplicationContext().getString(stringId);

		mNotificationManager = (NotificationManager)
				this.getSystemService(Context.NOTIFICATION_SERVICE);

		Intent newIntent = new Intent(this, SplashActivity.class);
		newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				newIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		NotificationCompat.Builder mBuilder =
				new NotificationCompat.Builder(this)
		.setSmallIcon(R.drawable.ic_launcher)
		.setContentTitle(msg)
		.setDefaults(Notification.DEFAULT_ALL)
		.setStyle(new NotificationCompat.BigTextStyle()
		.bigText(msg))
		.setContentText("New updates for " + appName);

		mBuilder.setContentIntent(contentIntent);
		mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

		Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
		r.play();

	}
 
Example 8
Source File: NotificationUtils.java    From protrip with MIT License 5 votes vote down vote up
public void playNotificationSound() {
    try {
        Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                + "://" + mContext.getPackageName() + "/raw/notification");
        Ringtone r = RingtoneManager.getRingtone(mContext, alarmSound);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: NotificationService.java    From Ecommerce-Retronight-Android with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
public void showNotification(String msg) {

		int stringId = getApplicationContext().getApplicationInfo().labelRes;
		String appName = getApplicationContext().getString(stringId);

		mNotificationManager = (NotificationManager)
				this.getSystemService(Context.NOTIFICATION_SERVICE);

		Intent newIntent = new Intent(this, SplashActivity.class);
		newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				newIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		NotificationCompat.Builder mBuilder =
				new NotificationCompat.Builder(this)
		.setSmallIcon(R.drawable.ic_launcher)
		.setContentTitle(msg)
		.setDefaults(Notification.DEFAULT_ALL)
		.setStyle(new NotificationCompat.BigTextStyle()
		.bigText(msg))
		.setContentText("New updates for " + appName);

		mBuilder.setContentIntent(contentIntent);
		mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

		Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
		r.play();

	}
 
Example 10
Source File: SystemUtil.java    From Android with MIT License 5 votes vote down vote up
/**
 * play system sound
 * @param context
 */
public static void noticeVoice(Context context) {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(context, notification);
    if (r != null) {
        r.play();
    }
}
 
Example 11
Source File: BreakFinishService.java    From SimplePomodoro-android with MIT License 5 votes vote down vote up
private void initiService(){
		SettingUtility.setRunningType(SettingUtility.BREAK_FINISHED);
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
		String strRingtonePreference = sp.getString("pref_notification_sound", "");
		if(!strRingtonePreference.equals("")){
			Uri ringtoneUri = Uri.parse(strRingtonePreference);
			Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
			ringtone.play();
//			String name = ringtone.getTitle(context);
		}
		Boolean isVibrator = sp.getBoolean("pref_enable_vibrations", false);
		if(isVibrator){
			Vibrator mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
			mVibrator.vibrate(new long[]{50,100,50,100}, -1);
		}
		MyUtils.ScreenState screenState = MyUtils.getScreenState(this);
		Intent intent = new Intent(BreakFinishService.this,BreakFinishActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		switch (screenState) {
		case LOCK:
			//prevent
			//if BreakActivity behind the lockScreen, the screen will not be waked up
//			Log.e("FinishService","LOCKing");
			startActivity(intent);
			break;
		case MYAPP:
			startActivity(intent);
			break;
		case OTHERAPP:
			showAlertDialog();
			break;
		default:
			break;
		}
		BreakFinishService.this.stopSelf();
	}
 
Example 12
Source File: FullScannerFragment.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
@Override
public void handleResult(Result rawResult)
{
    try
    {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getActivity(), notification);
        r.play();
    } catch (Exception e) {}

    Intent intent = new Intent();
    intent.putExtra(BarcodeObject, rawResult.getText());
    getActivity().setResult(SUCCESS, intent);
    getActivity().finish();
}
 
Example 13
Source File: Erc20DetailActivity.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private void playNotification()
{
    try
    {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(this, notification);
        r.play();
    }
    catch (Exception e)
    {
        //empty
    }
}
 
Example 14
Source File: QmsNewMessagesReceiver.java    From 4pdaClient-plus with Apache License 2.0 5 votes vote down vote up
private void playNotification(Context context) {
    try {

        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();
    } catch (Exception e) {
    }
}
 
Example 15
Source File: PbConversationManagePresenter.java    From imsdk-android with MIT License 5 votes vote down vote up
private void ringtone(){
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(CommonConfig.globalContext, notification);
        r.play();

    } catch (Exception e) {
    }
}
 
Example 16
Source File: Toast.java    From RobotHelper with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 声音提示
 */
public static void notice() {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(MainApplication.getInstance(), notification);
    r.play();
    Vibrator vibrator = (Vibrator) MainApplication.getInstance().getSystemService(VIBRATOR_SERVICE);
    vibrator.vibrate(1000);
}
 
Example 17
Source File: Utilities.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Ring action.
 *
 * @param context if something goes wrong.
 */
public static void ring(Context context) {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(context, notification);
    r.play();

}
 
Example 18
Source File: Utils.java    From android-wear-gopro-remote with Apache License 2.0 4 votes vote down vote up
public static void playTone(Context context) {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), notification);
    r.play();
}
 
Example 19
Source File: MainActivity.java    From Android-9-Development-Cookbook with MIT License 4 votes vote down vote up
public void clickSound(View view) {
    Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(),
            notificationSoundUri);
    ringtone.play();
}
 
Example 20
Source File: DockObserver.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void handleDockStateChange() {
    synchronized (mLock) {
        Slog.i(TAG, "Dock state changed from " + mPreviousDockState + " to "
                + mReportedDockState);
        final int previousDockState = mPreviousDockState;
        mPreviousDockState = mReportedDockState;

        // Skip the dock intent if not yet provisioned.
        final ContentResolver cr = getContext().getContentResolver();
        if (Settings.Global.getInt(cr,
                Settings.Global.DEVICE_PROVISIONED, 0) == 0) {
            Slog.i(TAG, "Device not provisioned, skipping dock broadcast");
            return;
        }

        // Pack up the values and broadcast them to everyone
        Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
        intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
        intent.putExtra(Intent.EXTRA_DOCK_STATE, mReportedDockState);

        boolean dockSoundsEnabled = Settings.Global.getInt(cr,
                Settings.Global.DOCK_SOUNDS_ENABLED, 1) == 1;
        boolean dockSoundsEnabledWhenAccessibility = Settings.Global.getInt(cr,
                Settings.Global.DOCK_SOUNDS_ENABLED_WHEN_ACCESSIBILITY, 1) == 1;
        boolean accessibilityEnabled = Settings.Secure.getInt(cr,
                Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1;

        // Play a sound to provide feedback to confirm dock connection.
        // Particularly useful for flaky contact pins...
        if ((dockSoundsEnabled) ||
               (accessibilityEnabled && dockSoundsEnabledWhenAccessibility)) {
            String whichSound = null;
            if (mReportedDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
                if ((previousDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
                    (previousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
                    (previousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
                    whichSound = Settings.Global.DESK_UNDOCK_SOUND;
                } else if (previousDockState == Intent.EXTRA_DOCK_STATE_CAR) {
                    whichSound = Settings.Global.CAR_UNDOCK_SOUND;
                }
            } else {
                if ((mReportedDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
                    (mReportedDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
                    (mReportedDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
                    whichSound = Settings.Global.DESK_DOCK_SOUND;
                } else if (mReportedDockState == Intent.EXTRA_DOCK_STATE_CAR) {
                    whichSound = Settings.Global.CAR_DOCK_SOUND;
                }
            }

            if (whichSound != null) {
                final String soundPath = Settings.Global.getString(cr, whichSound);
                if (soundPath != null) {
                    final Uri soundUri = Uri.parse("file://" + soundPath);
                    if (soundUri != null) {
                        final Ringtone sfx = RingtoneManager.getRingtone(
                                getContext(), soundUri);
                        if (sfx != null) {
                            sfx.setStreamType(AudioManager.STREAM_SYSTEM);
                            sfx.play();
                        }
                    }
                }
            }
        }

        // Send the dock event intent.
        // There are many components in the system watching for this so as to
        // adjust audio routing, screen orientation, etc.
        getContext().sendStickyBroadcastAsUser(intent, UserHandle.ALL);
    }
}