如何将原始音频文件设置为铃声

how to set raw audio file as ringtone

所以我正在尝试将位于 raw 中的音频文件设置为铃声,然后将其保存到我的 sdcard。当前代码似乎保存了一个文件,但该文件播放了一些通用铃声,而不是我试图让它播放的声音。它有什么问题?

            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();

试试这个

            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) {

        }