Android/MonoDroid 自定义铃声问题

发布于 2024-10-05 03:49:48 字数 328 浏览 2 评论 0原文

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

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

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

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

有人知道吗这是如何实现的?我到处寻找,却没有弄清楚。谢谢

I am trying to figure out how to take an audio file from the Assets folder (included as an AndroidAsset) and add it to the list of Ringtones you see when you make this call:

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

I am adding the ringtone via this call:

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

Does anyone know how this is accomplished? I have been searching all over and haven’t figured it out. Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-10-12 03:49:48

像这样的东西:

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

Something like this:

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) {
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文