Android / MonoDroid自定义铃声问题

时间:2010-12-01 02:12:33

标签: android android-emulator xamarin.android

我正在尝试弄清楚如何从Assets文件夹中获取音频文件(包含在AndroidAsset中)并将其添加到您拨打此电话时看到的铃声列表中:

this.StartActivity(new Intent(Android.Media.RingtoneManager.ActionRingtonePicker));

我正在通过此电话添加铃声:

InputStream inputstream = Assets.Open("filename.mp3");

有谁知道这是如何实现的?我一直在寻找,并没有想出来。谢谢

1 个答案:

答案 0 :(得分:1)

这样的事情:

private void setAsRingtone(){
        try {
            //Open the InputStream from the Assets
            InputStream fis = Assets.Open("filename.mp3");
            if (fis == null)
                return;

            //Open a File to save the ringtone in the SD (/sdcard/Android/data/com.your.package/)
            File path = new
            File(Environment.getExternalStorageDirectory().getAbsolutePath() +
            "/Android/data/com.your.package/");
            if(!path.exists())
                path.mkdirs();

            //Create the proper file
            File f = new File(path, "YourTitle" + ".mp3");

            //Dump the InputStream in the File
            OutputStream fos = new FileOutputStream(f);
            byte[] buf =new byte[1024];
            int len;
            while((len=fis.read(buf))>0){
                fos.write(buf,0,len);
            }
            fos.close();
            fis.close();

            //Here are the metadata of the ringtone
            ContentValues values = new ContentValues();
            values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "YourTitle");
            values.put(MediaStore.MediaColumns.SIZE, f.length());
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.ARTIST, "YourArtist");
            //values.put(MediaStore.Audio.Media.DURATION, ""); This is not needed
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
            values.put(MediaStore.Audio.Media.IS_ALARM, false);
            values.put(MediaStore.Audio.Media.IS_MUSIC, false);

            //We put in the DDBB of MediaStore
            Uri uri =
                MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());
            Uri newUri = getBaseContext().getContentResolver().insert(uri, values);

            //Set as default
                RingtoneManager.setActualDefaultRingtoneUri(
                        getBaseContext(),
                        RingtoneManager.TYPE_RINGTONE,
                        newUri);

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }