在 Android 中设置铃声

发布于 2024-08-16 16:52:16 字数 266 浏览 12 评论 0原文

可能的重复:
如何通过我的 Activity 在 Android 中设置铃声?

我的 res/raw 文件夹中有声音文件,我想通过单击按钮选择一个声音设置为铃声。想知道我该怎么做?

Possible Duplicate:
How to set ringtone in Android from my activity?

I have sounds files in my res/raw folder and i want to select a sound to set as a ringtone on the click of a button. Wonder how can i do that?

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

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

发布评论

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

评论(4

木緿 2024-08-23 16:52:16

@Maxood

@Clive 的代码是您设置铃声所需的代码。您将需要文件的绝对路径,而您无法从原始资源中获得该路径。

解决方案是先获取资源文件 asset 并将其写入 SD 卡,然后再将其交给内容解析器进行插入。

File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog");
Uri mUri = Uri.parse("android.resource://com.your.package/R.raw.your_resource_id");
ContentResolver mCr = app.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(newSoundFile);
      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, newSoundFile.getAbsolutePath());
   values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
   values.put(MediaStore.MediaColumns.SIZE, newSoundFile.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, false);

   Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
   Uri newUri = mCr.insert(uri, values);


   try {
       RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
   } catch (Throwable t) {
       Log.d(TAG, "catch exception");
   }

不要忘记在清单中写入权限

希望

这会有所帮助

@Maxood

The code from @Clive is what you need to set the ringtone. You will need the absolute path to the file, which you can't get from a raw resource.

The solution is to get the resource file asset and write it to the sdcard 1st, before you give it to the content resolver for insertion.

File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog");
Uri mUri = Uri.parse("android.resource://com.your.package/R.raw.your_resource_id");
ContentResolver mCr = app.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(newSoundFile);
      int i = fis.read(readData);

      while (i != -1) {
        fos.write(readData, 0, i);
        i = fis.read(readData);
      }

      fos.close();
   } catch (IOException io) {
   }

Then you can use the previously posted solution

       ContentValues values = new ContentValues();
   values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
   values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
   values.put(MediaStore.MediaColumns.SIZE, newSoundFile.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, false);

   Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
   Uri newUri = mCr.insert(uri, values);


   try {
       RingtoneManager.setActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_RINGTONE, newUri);
   } catch (Throwable t) {
       Log.d(TAG, "catch exception");
   }

Don't forget to write the the permission

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

in your manifest

hope this helps

挽手叙旧 2024-08-23 16:52:16

试试这个,它对我有用:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, <<asbolutePathToYourAudioFileHere>>);
values.put(MediaStore.MediaColumns.TITLE, "<<yourRingToneNameHere>>");
values.put(MediaStore.MediaColumns.SIZE, k);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");    // assuming it's an mpeg, of course
values.put(MediaStore.Audio.Media.ARTIST, "<<yourArtistNameHere>>");
// values.put(MediaStore.Audio.Media.DURATION, duration);  // doesn't appear to be necessary if you don't know
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);  
Uri newUri = getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
                                <<MyActivity>>.this,
                                RingtoneManager.TYPE_RINGTONE,
                                newUri);

Try this, it works for me:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, <<asbolutePathToYourAudioFileHere>>);
values.put(MediaStore.MediaColumns.TITLE, "<<yourRingToneNameHere>>");
values.put(MediaStore.MediaColumns.SIZE, k);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");    // assuming it's an mpeg, of course
values.put(MediaStore.Audio.Media.ARTIST, "<<yourArtistNameHere>>");
// values.put(MediaStore.Audio.Media.DURATION, duration);  // doesn't appear to be necessary if you don't know
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath);  
Uri newUri = getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(
                                <<MyActivity>>.this,
                                RingtoneManager.TYPE_RINGTONE,
                                newUri);
离鸿 2024-08-23 16:52:16

希望现在您已经让您的程序按照您想要的方式运行。不过,仅供记录,您应该考虑将文件保存到 SD 卡的铃声文件夹下。确保它是小写的,因为这在 Android 中很重要。

Hopefully by now you have gotten your program working the way you wanted. Just for the record though, you should look into saving the file to the sdcard under a ringtones folder. Make sure it is lower cased as that does matter in Android.

蔚蓝源自深海 2024-08-23 16:52:16

我使用“Rings Extended” http://www.androidapps.com/t/rings-extended< /a>

安装该应用程序后,当您要更改铃声时,您可以选择选择铃声扩展。还可以使用“Ringdroid”编辑铃声。

I use "Rings Extended" http://www.androidapps.com/t/rings-extended

With that app installed when you go to change your ringtone you will have the option to select Rings Extended. Also use "Ringdroid" to edit ringtones.

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