下面列出了怎么用android.media.Ringtone的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Beep plays the default notification ringtone.
*
* @param count Number of times to play notification
*/
public void beep(final long count) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone notification = RingtoneManager.getRingtone(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) {
}
}
}
}
}
});
}
public static Ringtone getRingtone(Context context) {
Uri alert =
RingtoneManager
.getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_ALARM);
if (alert == null)
alert = RingtoneManager
.getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_NOTIFICATION);
if (alert == null)
alert = RingtoneManager
.getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
final Ringtone ringtone = RingtoneManager.getRingtone(context, alert);
final AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
if (audioManager != null) {//who knows lol - btw don't delete user's may lower the alarm sounds by mistake
final int alarmVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM) * (BPrefs.get(context).getInt(BPrefs.ALARM_VOLUME_KEY, BPrefs.ALARM_VOLUME_DEFAULT_VALUE) + 6) / 10;
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, alarmVolume, 0);
}
ringtone.setAudioAttributes(alarmAttributes);
return ringtone;
}
/**
* Beep plays the default notification ringtone.
*
* @param count Number of times to play notification
*/
public void beep(final long count) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone notification = RingtoneManager.getRingtone(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) {
}
}
}
}
}
});
}
private void updateRingtoneSummary(PreferenceScreen preferenceScreen) {
String name = getString(R.string.pref_ringtone_none);
String value = Preferences.getString(mContext, Preferences.NOTIFICATION_RINGTONE, null);
if (!TextUtils.isEmpty(value)) {
Uri ringtoneUri = Uri.parse(value);
Ringtone ringtone = RingtoneManager.getRingtone(mContext, ringtoneUri);
if (ringtone != null) {
name = ringtone.getTitle(mContext);
}
}
preferenceScreen.findPreference(Preferences.NOTIFICATION_RINGTONE).setSummary(name);
preferenceScreen.findPreference(Preferences.NOTIFICATION_RINGTONE).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String v = (String) newValue;
preference.setSummary(v);
return true;
}
});
}
/**
* Beep plays the default notification ringtone.
*
* @param count Number of times to play notification
*/
public void beep(final long count) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone notification = RingtoneManager.getRingtone(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) {
}
}
}
}
}
});
}
public String shortPath(String path) {
if(isPathRingtone(mContext, path)) {
Ringtone ringtone = RingtoneManager.getRingtone(mContext, Uri.parse(path));
// Just verified that the ringtone exists... not checking for null
return ringtone.getTitle(mContext);
}
if(path == null) {
return "";
}
if(path.length() == 0) {
return "xDrip Default";
}
String[] segments = path.split("/");
if (segments.length > 1) {
return segments[segments.length - 1];
}
return path;
}
/**
* Beep plays the default notification ringtone.
*
* @param count Number of times to play notification
*/
public void beep(final long count) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone notification = RingtoneManager.getRingtone(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) {
Thread.currentThread().interrupt();
}
}
}
}
}
});
}
public String shortPath(String path) {
if(path != null) {
if(path.length() == 0) {
return "xDrip Default";
}
Ringtone ringtone = RingtoneManager.getRingtone(mContext, Uri.parse(path));
if (ringtone != null) {
return ringtone.getTitle(mContext);
} else {
String[] segments = path.split("/");
if (segments.length > 1) {
return segments[segments.length - 1];
}
}
}
return "";
}
private void buildSummary() {
if (SettingUtils.getEnableFetchMSG()) {
String value = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(SettingActivity.FREQUENCY,
"1");
frequency.setSummary(getActivity().getResources().getStringArray(R.array.frequency)[Integer.valueOf(value) - 1]);
} else {
frequency.setSummary(getString(R.string.stopped));
}
if (uri != null) {
Ringtone r = RingtoneManager.getRingtone(getActivity(), uri);
ringtone.setSummary(r.getTitle(getActivity()));
} else {
ringtone.setSummary(getString(R.string.silent));
}
}
/**
* Get the title of the ringtone from the uri of ringtone.
*
* @param context instance of the caller
* @param uri uri of the tone to search
* @return title of the tone or return null if no tone found.
*/
@Nullable
public static String getRingtoneName(@NonNull final Context context,
@NonNull final Uri uri) {
final Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
if (ringtone != null) {
return ringtone.getTitle(context);
} else {
Cursor cur = context
.getContentResolver()
.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.TITLE},
MediaStore.Audio.Media._ID + " =?",
new String[]{uri.getLastPathSegment()},
null);
String title = null;
if (cur != null) {
title = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.TITLE));
cur.close();
}
return title;
}
}
public String shortPath(String path) {
if(path != null) {
if(path.length() == 0) {
return "xDrip Default";
}
Ringtone ringtone = RingtoneManager.getRingtone(mContext, Uri.parse(path));
if (ringtone != null) {
return ringtone.getTitle(mContext);
} else {
String[] segments = path.split("/");
if (segments.length > 1) {
return segments[segments.length - 1];
}
}
}
return "";
}
public String shortPath(String path) {
if(path != null) {
if(path.length() == 0) {
return "xDrip Default";
}
Ringtone ringtone = RingtoneManager.getRingtone(mContext, Uri.parse(path));
if (ringtone != null) {
return ringtone.getTitle(mContext);
} else {
String[] segments = path.split("/");
if (segments.length > 1) {
return segments[segments.length - 1];
}
}
}
return "";
}
public void setMathAlarm(Alarm alarm) {
this.alarm = alarm;
preferences.clear();
// preferences.add(new AlarmPreference(Key.ALARM_ACTIVE, context.getString(R.string.AlarmStatus), null, null, alarm.getAlarmActive(), Type.BOOLEAN));
preferences.add(new AlarmPreference(Key.ALARM_NAME, "标签", alarm.getAlarmName(), null, alarm.getAlarmName(), Type.EditText));
preferences.add(new AlarmPreference(Key.ALARM_TIME, "时间", alarm.getAlarmTimeString(), null, alarm.getAlarmTime(), Type.TIME));
preferences.add(new AlarmPreference(Key.ALARM_REPEAT, "重复", "重复", repeatDays, alarm.getDays(), Type.MULTIPLE_ImageButton));
Uri alarmToneUri = Uri.parse(alarm.getAlarmTonePath());
Ringtone alarmTone = RingtoneManager.getRingtone(getContext(), alarmToneUri);
if (alarmTone instanceof Ringtone && !alarm.getAlarmTonePath().equalsIgnoreCase("")) {
preferences.add(new AlarmPreference(Key.ALARM_TONE, "铃声", alarmTone.getTitle(getContext()), alarmTones, alarm.getAlarmTonePath(), Type.Ring));
} else {
preferences.add(new AlarmPreference(Key.ALARM_TONE, "铃声", getAlarmTones()[0], alarmTones, null, Type.Ring));
}
preferences.add(new AlarmPreference(Key.ALARM_VIBRATE, "振动", null, null, alarm.IsVibrate(), Type.BOOLEAN));
}
private String shortPath(String path) {
try {
if (path != null) {
if (path.length() == 0) {
return "xDrip Default";
}
Ringtone ringtone = RingtoneManager.getRingtone(mContext, Uri.parse(path));
if (ringtone != null) {
return ringtone.getTitle(mContext);
} else {
String[] segments = path.split("/");
if (segments.length > 1) {
return segments[segments.length - 1];
}
}
}
return "";
} catch (SecurityException e) {
// need external storage permission?
checkStoragePermissions(gs(R.string.need_permission_to_access_audio_files));
return "";
}
}
/**
* Beep plays the default notification ringtone.
*
* @param count Number of times to play notification
*/
public void beep(final long count) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone notification = RingtoneManager.getRingtone(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) {
Thread.currentThread().interrupt();
}
}
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
Ringtone ringTone = RingtoneManager.getRingtone(getApplicationContext(), uri);
ringtone_name.setText(ringTone.getTitle(this));
switch (requestCode) {
case REQUEST_TONE_PICKER_CUSTOM:
mSettingsHelper.setString("custom_ringtone", uri.toString());
break;
case REQUEST_TONE_PICKER_NEW_COMMENT:
mSettingsHelper.setString("new_comment_ringtone", uri.toString());
break;
case REQUEST_TONE_PICKER_MSG_RECALL:
mSettingsHelper.setString("msg_recall_ringtone", uri.toString());
break;
case REQUEST_TONE_PICKER_COMMENT_RECALL:
mSettingsHelper.setString("comment_recall_ringtone", uri.toString());
break;
}
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Uri value = (Uri) newValue;
if (value == null || TextUtils.isEmpty(value.toString())) {
preference.setSummary(R.string.pref_silent);
} else {
Ringtone tone = RingtoneManager.getRingtone(getActivity(), value);
if (tone != null) {
preference.setSummary(tone.getTitle(getActivity()));
}
}
return true;
}
/**
* 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) {
}
}
}
}
}
static void setRingtonePreferenceSummary(final String initSummary, final String ringtoneUri,
final androidx.preference.Preference preference, final Context context) {
new AsyncTask<Void, Integer, Void>() {
private String ringtoneName;
@Override
protected Void doInBackground(Void... params) {
if ((ringtoneUri == null) || ringtoneUri.isEmpty())
ringtoneName = context.getString(R.string.ringtone_preference_none);
else {
Uri uri = Uri.parse(ringtoneUri);
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
try {
ringtoneName = ringtone.getTitle(context);
} catch (Exception e) {
ringtoneName = context.getString(R.string.ringtone_preference_not_set);
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
String summary = TextUtils.replace(initSummary, new String[]{"<ringtone_name>"}, new String[]{ringtoneName}).toString();
preference.setSummary(GlobalGUIRoutines.fromHtml(summary, false, false, 0, 0));
}
}.execute();
}
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 static boolean isPathRingtone(Context context, String path) {
if(path == null) {
return false;
}
if(path.length() == 0) {
return false;
}
Ringtone ringtone = RingtoneManager.getRingtone(context, Uri.parse(path));
if(ringtone == null) {
return false;
}
return true;
}
/**
* 声音提示
*/
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);
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
if (TextUtils.isEmpty(stringValue)) {
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
preference.setSummary(null);
} else {
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
preference.setSummary(stringValue);
}
return true;
}
@RequiresApi(api = Build.VERSION_CODES.O)
static NotificationChannel channelFromJSON(Context context, JSONObject jchannel) throws JSONException {
NotificationChannel channel = new NotificationChannel(
jchannel.getString("id"),
jchannel.getString("name"),
jchannel.getInt("importance"));
String group = jchannel.optString("group");
if (!TextUtils.isEmpty(group))
channel.setGroup(group);
if (jchannel.has("description") && !jchannel.isNull("description"))
channel.setDescription(jchannel.getString("description"));
channel.setBypassDnd(jchannel.getBoolean("dnd"));
channel.setLockscreenVisibility(jchannel.getInt("visibility"));
channel.setShowBadge(jchannel.getBoolean("badge"));
if (jchannel.has("sound") && !jchannel.isNull("sound")) {
Uri uri = Uri.parse(jchannel.getString("sound"));
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
if (ringtone != null)
channel.setSound(uri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
}
channel.enableLights(jchannel.getBoolean("light"));
channel.enableVibration(jchannel.getBoolean("vibrate"));
return channel;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE_NOTIFICATION_PICKER &&
resultCode == RESULT_OK)
{
Uri notificationToneUri = (Uri) data.getExtras().get(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
//todo save the value to shared preferences
EasySettings.retrieveSettingsSharedPrefs(this)
.edit()
.putString(ActivityMain.SETTINGS_KEY_RINGTONE,
//if notificationToneUri is null, it means "silent"
//was chosen.
notificationToneUri == null ? SETTINGS_RINGTONE_SILENT_VALUE
: notificationToneUri.toString())
.apply();
String soundTitle;
//"silent" was chosen
if(notificationToneUri == null)
{
soundTitle = DEFAULT_NOTIFICATION_SUMMARY;
}
else
{
Ringtone ringtone = RingtoneManager.getRingtone(this, notificationToneUri);
soundTitle = ringtone.getTitle(this);
}
if(tvNotificationToneSummary != null)
{
tvNotificationToneSummary.setText(soundTitle);
}
}
}
@Override
public void onClick(View v) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone ringtone = RingtoneManager.getRingtone(getActivity(),notification);
ringtone.play();
}
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 playNotification(Context context) {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
} catch (Exception e) {
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
} else if (preference instanceof RingtonePreference) {
if (TextUtils.isEmpty(stringValue)) {
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
preference.setSummary(null);
} else {
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
preference.setSummary(stringValue);
}
return true;
}