How to set Ringtone through nativescript?


#1

It seems there is no plugin available for setting ringtone through nativescript, what are the other methods of doing this?


Ringtone Samsung
#2

You can directly access the android native APIs to set your ringtone.

Permissions:
You need android.permission.WRITE_SETTINGS permission in your manifest for Android API Level < 23. For above ones you need to use the following,

if (android.provider.Settings.System.canWrite(application.android.context)) {
  // write your code here
} else {
  const intent = new android.content.Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
  intent.setData(android.net.Uri.parse("package:" + application.android.foregroundActivity.getPackageName()));
  intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
  application.android.foregroundActivity.startActivity(intent);
}

To set ringtone:

const file = new java.io.File(filePath);

const values = new android.content.ContentValues();
values.put(android.provider.MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(android.provider.MediaStore.MediaColumns.TITLE, "Show me the meaning");
values.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(android.provider.MediaStore.Audio.AudioColumns.ARTIST, "None");
values.put(android.provider.MediaStore.MediaColumns.SIZE, new java.lang.Integer(file.length()));
values.put(android.provider.MediaStore.Audio.AudioColumns.IS_RINGTONE, new java.lang.Boolean(true));
values.put(android.provider.MediaStore.Audio.AudioColumns.IS_NOTIFICATION, new java.lang.Boolean(false));
values.put(android.provider.MediaStore.Audio.AudioColumns.IS_ALARM, new java.lang.Boolean(false));
values.put(android.provider.MediaStore.Audio.AudioColumns.IS_MUSIC, new java.lang.Boolean(false));

// Insert it into the database
const contentResolver = application.android.foregroundActivity.getContentResolver();
const uri = android.provider.MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
const newUri = contentResolver.insert(uri, values);

android.media.RingtoneManager
  .setActualDefaultRingtoneUri(application.android.foregroundActivity, 
android.media.RingtoneManager.TYPE_RINGTONE, newUri);

Note: Whenever you call contentResolver.insert, it will create a new entry. Make sure you delete unused / unwanted entries.


#3

Hi Sir,
Me too have the same problem and have already tried with these codes. But it doen’t seem to work on javascript. Could you please tell what is the exact javascript code for setting ringtone in nativescript?
I have searched a lot for these, but could not find . I am a new bie to nativescript, so kindly help.


#4

In general, even when you use TypeScript (with or without Angular or Vue frameworks) for development, end of the day it is transpiled to JavaScript. So it makes no difference, I had tested this code in Android 7.x and TNS 3.3.1 already.

If you are still facing issues, please post your error log.


#5

Hi Sir,
Thanks ! It works :slight_smile: But my ringtone is being set to “None”, instead of the file I have choosen.
Previously this too didn’t happened, because of the wrong syntax of codes I have written.
Could you please tell why it is set to “None”?


#6

It will be none only if the content uri is invalid / if it is not accessible. If you have your ringtone file in your app storage, try moving it to external storage.