【问题标题】:Adding a mp3 from my res/raw of package into the Ringtone List of Android将我的 res/raw 包中的 mp3 添加到 Android 的铃声列表中
【发布时间】:2011-07-26 16:13:31
【问题描述】:

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

【问题讨论】:

    标签: android audio notifications mp3 ringtone


    【解决方案1】:

    如果您不介意要复制的文件,那么将其包含在内的一种简单方法是让您的应用将文件放在 SD 卡上的以下位置:

    [/sdcard]/媒体/铃声

    【讨论】:

    • 我不介意它被复制,但我如何将它从我的 res/raw 复制到 sdcard
    【解决方案2】:

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

    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 文件复制到 notification 文件夹。这是命名约定。然后当你像这样设置铃声选择器时

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

    只有当文件位于名称为 notifications 的文件夹中时,您才会看到文件。

    更新:

    忘了提到你应该在添加一些新的铃声/通知后扫描你的 SD 卡。比如这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多