在 Android 中设置铃声
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1986756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Ringtone in Android
提问by Muhammad Maqsoodur Rehman
Possible Duplicate:
How to set ringtone in Android from my activity?
可能的重复:
如何通过我的活动在 Android 中设置铃声?
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?
我的 res/raw 文件夹中有声音文件,我想通过单击按钮选择一种声音设置为铃声。想知道我该怎么做?
回答by Chiatar
@Maxood
@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.
来自@Clive 的代码是您设置铃声所需的代码。您将需要文件的绝对路径,而您无法从原始资源中获取该路径。
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.
解决方案是获取资源文件资产并将其写入sdcard 1st,然后再将其提供给内容解析器进行插入。
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"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in your manifest
在你的清单中
hope this helps
希望这可以帮助
回答by Clive
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);
回答by NetApex
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.
希望现在你已经让你的程序按照你想要的方式工作了。只是为了记录,您应该考虑将文件保存到铃声文件夹下的 SD 卡中。确保它是小写的,因为这在 Android 中很重要。
回答by SuperJames
I use "Rings Extended" http://www.androidapps.com/t/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.
安装该应用程序后,您可以在更改铃声时选择 Rings Extended。还可以使用“Ringdroid”来编辑铃声。