This code will set your ringtone for ANDROID.
String filepath = "/sdcard/myring.mp3" ; File ringtoneFile = new File(filepath); |
To set a ringtone you have to add it to the database.
otherwise it will not be set also gives no error also.
ContentValues content = new ContentValues(); content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath()); content.put(MediaStore.MediaColumns.TITLE, "test" ); content.put(MediaStore.MediaColumns.SIZE, 215454 ); content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*" ); content.put(MediaStore.Audio.Media.ARTIST, "artist" ); content.put(MediaStore.Audio.Media.DURATION, 230 ); content.put(MediaStore.Audio.Media.IS_RINGTONE, true ); content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false ); content.put(MediaStore.Audio.Media.IS_ALARM, false ); content.put(MediaStore.Audio.Media.IS_MUSIC, false ); |
Insert it into the database
Log.i(TAG, "the absolute path of the file is :" +ringtoneFile.getAbsolutePath()); Uri uri = MediaStore.Audio.Media.getContentUriForPath( ringtoneFile.getAbsolutePath()); Uri newUri = context.getContentResolver().insert(uri, content); ringtoneUri = newUri; Log.i(TAG, "the ringtone uri is :" +ringtoneUri); RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE,newUri); |
Hello ,
I Am Newbie In Flutter Framework, I Am Working On Flutter Project In Which I Want To Set MP3 File As A Ringtone For Flutter App’s. If There Is Any Way Let Me Know As Soon As Possible.
Thanks …
iOS wont allow you to do that programatically.
Hello, Gunman. I am also trying to set ringtone pragmatically using flutter. Did you manage to achieve it you guide me?