Search code examples
androidaudioringtoneringtonemanager

how to set raw audio file as ringtone


So i'm trying to set an audio file located in the raw as a ringtone and then saving it to my sdcard. The current code seems to save a file, but that file plays some generic ringtone as opposed to the sound I'm trying to get it to play. What's the issue with it?

            String exStorePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            String path = exStorePath + "/media/ringtone/";

            File k = new File(path, "wearenumberone.mp3");

            Uri mUri = Uri.parse("android.resource://com.example.matig.mlgsoundboarddeluxe/" + R.raw.wearenumberone);
            ContentResolver mCr = SoundActivity1.this.getContentResolver();

            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "NUMBERONE2");
            values.put(MediaStore.MediaColumns.SIZE, k.length());
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, "guy");
            values.put(MediaStore.Audio.Media.DURATION, 230);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, false);

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
            Uri newUri = mCr.insert(uri, values);

            RingtoneManager.setActualDefaultRingtoneUri(SoundActivity1.this, RingtoneManager.TYPE_RINGTONE, newUri);
            Settings.System.putString(mCr,Settings.System.RINGTONE,newUri.toString());
            Toast.makeText(SoundActivity1.this,"done",Toast.LENGTH_SHORT).show();

Solution

  • try this

                File file = new File(Environment.getExternalStorageDirectory(),
                    "/myRingtonFolder/Audio/");
            if (!file.exists()) {
                file.mkdirs();
            }
    
            String path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/myRingtonFolder/Audio/";
    
            File f = new File(path + "/", name + ".mp3");
    
            Uri mUri = Uri.parse("android.resource://"
                    + context.getPackageName() + "/raw/" + name);
            ContentResolver mCr = context.getContentResolver();
            AssetFileDescriptor soundFile;
            try {
                soundFile = mCr.openAssetFileDescriptor(mUri, "r");
            } catch (FileNotFoundException e) {
                soundFile = null;
            }
    
            try {
                byte[] readData = new byte[1024];
                FileInputStream fis = soundFile.createInputStream();
                FileOutputStream fos = new FileOutputStream(f);
                int i = fis.read(readData);
    
                while (i != -1) {
                    fos.write(readData, 0, i);
                    i = fis.read(readData);
                }
    
                fos.close();
            } catch (IOException io) {
            }
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, name);
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.MediaColumns.SIZE, f.length());
            values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
            values.put(MediaStore.Audio.Media.IS_ALARM, true);
            values.put(MediaStore.Audio.Media.IS_MUSIC, true);
    
            Uri uri = MediaStore.Audio.Media.getContentUriForPath(f
                    .getAbsolutePath());
            Uri newUri = mCr.insert(uri, values);
    
            try {
                RingtoneManager.setActualDefaultRingtoneUri(context,
                        RingtoneManager.TYPE_RINGTONE, newUri);
                Settings.System.putString(mCr, Settings.System.RINGTONE,
                        newUri.toString());
            } catch (Throwable t) {
    
            }