I have two buttons in my app. One for setting a sound file as ringtone and another as notification. I am able to set the sound file as a ringtone but when I try to set it as
Try this snippet and get red off Ringtone Setting Up
public boolean setAsRingtone(String filePath) {
// this function is use to On or Off Default Ringtone
String[] files = { filePath };
MediaScannerConnection.scanFile(context, files, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
System.out.println("Ringtone file " + path + " was scanned seccessfully: " + uri);
if(path!=null&&!path.isEmpty()){
try {
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri);
} catch (Throwable t) {
}
}else {
String defaultPath = Settings.System.DEFAULT_RINGTONE_URI.getPath();
File newSoundFile = new File(defaultPath);
Uri newUri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
try {
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
} catch (Throwable t) {}
}
}
}
);
Toast.makeText(context, "Ringtone has been set successfully", Toast.LENGTH_SHORT).show();
return true;
}