将我的 res/raw 包中的 mp3 添加到 Android 的铃声列表中

发布于 2024-11-26 10:48:06 字数 89 浏览 3 评论 0原文

如何将我的应用程序 res/raw 中的 mp3 添加到 Android 铃声列表中。 我不想播放它,而是将其添加到铃声列表中。 已经阅读了一些主题,但仍然很困惑。

How do I add the mp3 in my res/raw of application into the Android ringtone list.
I dont want to play it, but to add it into the ringtone list.
Been reading some topics but still very confused.

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

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

发布评论

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

评论(2

九厘米的零° 2024-12-03 10:48:06

如果您不介意复制文件,则包含该文件的一个简单方法是让您的应用程序将文件拖放到 SD 卡上的以下位置:

[/sdcard]/media/ringtones

If you do not mind the file being copied, then an easy way to have it included is to have your app drop the file on the SD card under:

[/sdcard]/media/ringtones

清欢 2024-12-03 10:48:06

只需将所有铃声添加到某个文件夹(必须首先创建文件夹)

InputStream in = getResources().openRawResource(R.raw.ringtone1);
FileOutputStream out = new FileOutputStream("/mnt/sdcard/media/ringtones/ringtone1.mp3");
byte[] buff = new byte[1024];
int read = 0;

try {
   while ((read = in.read(buff)) > 0) {
      out.write(buff, 0, read);
   }
} finally {
     in.close();

     out.close();
}

不要忘记添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

如果您只想从铃声选择器中选择通知,您应该将原始 mp3 文件复制到通知文件夹。这是命名约定。然后,当您像这样设置铃声选择器时,

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
    RingtoneManager.TYPE_NOTIFICATION);

仅当文件位于名称为通知的文件夹中时,您才会看到您的文件。

更新:

忘记提及,在添加一些新的铃声/通知后,您应该扫描您的 SD 卡。例如这样:

    mContext.sendBroadcast (
            new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory()))
    );

Just add all your ringtones to some folder (folder must be created first)

InputStream in = getResources().openRawResource(R.raw.ringtone1);
FileOutputStream out = new FileOutputStream("/mnt/sdcard/media/ringtones/ringtone1.mp3");
byte[] buff = new byte[1024];
int read = 0;

try {
   while ((read = in.read(buff)) > 0) {
      out.write(buff, 0, read);
   }
} finally {
     in.close();

     out.close();
}

Don't forget to add permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

If you want to pick only notifications from Ringtone Picker you should copy your raw mp3 file to notification folder. It's naming convention. Then when you set up Ringtone Picker like this

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
    RingtoneManager.TYPE_NOTIFICATION);

You will see your file only if it's located in folder which name is notifications.

UPDATED:

Forgot to mention that you should scan your sdcard after adding some new ringtones/notifications. Like this for example:

    mContext.sendBroadcast (
            new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://" + Environment.getExternalStorageDirectory()))
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文