下面列出了android.media.Ringtone#play ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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) {
}
}
}
}
}
/**
* 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) {
}
}
}
}
}
@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();
}
}
}
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);
}
}
}
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();
}
/**
* 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();
}
/**
* 声音提示
*/
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);
}
private void ringtone(){
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(CommonConfig.globalContext, notification);
r.play();
} catch (Exception e) {
}
}
private void playNotification(Context context) {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
} catch (Exception e) {
}
}
private void playNotification()
{
try
{
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(this, notification);
r.play();
}
catch (Exception e)
{
//empty
}
}
@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();
}
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();
}
/**
* 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();
}
}
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();
}
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();
}
}
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();
}
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();
}
}
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);
}
}
public void clickSound(View view) {
Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(),
notificationSoundUri);
ringtone.play();
}
public static void playTone(Context context) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), notification);
r.play();
}