Android 添加工具以在应用程序中选择自定义音效
我有一个应用程序,它在应用程序生命周期的某些时刻播放一些声音。我想允许用户自定义这些声音(目前它们是位于 res/raw 目录中的 mp3 文件)。我正在查看 RingtonePreference,但不清楚如何向显示的值添加新声音,然后在我的应用程序中使用它们。如果有人能指出我正确的方向,我将非常感激。
谢谢你!
编辑:
一种方法似乎是将文件添加到媒体存储中。类似于下面所示的代码。这看起来确实有点矫枉过正,因为现在在 android 清单文件中你必须使用权限“android.permission.WRITE_SETTINGS”。理想情况下,我只想将 mp3 资源添加为选项,而不将它们写入媒体存储。
ContentValues content = new ContentValues();
InputStream raw = getResources()
.openRawResource(R.raw.file_name);
File f = new File("android.resource://" + getPackageName() + "/"
+ R.raw.clock_ticking);
content.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "TITLE");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
content.put(MediaStore.Audio.Media.ARTIST, "artist");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, false);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(f
.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, content);
编辑2: 另一种方法似乎是使用列表首选项。尽管列表首选项可以工作 - 当选择更改时,您必须手动“播放”声音。让我看看这是否有效,然后我会报告。
I have an app that plays some sounds at certain points in the app's life cycle. I want to allow the user to customize these sounds (currently they are mp3 files residing in res/raw directory). I am looking at RingtonePreference but it is not clear how I can add new sounds to the values shown and then use them in my application. If someone can point me in the right direction, I would really appreciate it.
Thank you!
EDIT:
One way seems to be to add the file into the media store. Something like the code shown below. This does seem a bit overkill since now in the android manifest file you have to use the permission "android.permission.WRITE_SETTINGS". Ideally, I would like to just add the mp3 resources as options without writing them into the media store.
ContentValues content = new ContentValues();
InputStream raw = getResources()
.openRawResource(R.raw.file_name);
File f = new File("android.resource://" + getPackageName() + "/"
+ R.raw.clock_ticking);
content.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "TITLE");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
content.put(MediaStore.Audio.Media.ARTIST, "artist");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, false);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(f
.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, content);
EDIT 2:
Another way seems to be use list preferences. Although list preferences would work - you would have to manually "play" the sound when the selection changes. Let me see if that works or not and I would report back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建某种文件资源管理器(您可以使用 File 类,与常规 Java 相同,创建它),以便允许用户选择文件,然后使用
MediaPlayer
来播放它们。编辑:
如果你想让它更容易,请让你的用户将它们放在SD卡的指定目录中,比如
yourapp/sounds
,然后你可以使用ListView使用以下内容列出该目录中的所有文件:Create some kind of file explorer (you can use the File class, same as regular Java, to create it) in order to allow the user to select a file and then use a
MediaPlayer
to play them.Edit:
If you want to make it easier, ask your users to put them in a specified directory in the SD Card, say
yourapp/sounds
, then you can use a ListView to list all the files in that directory using something as:对我有用的最佳解决方案是:
使用 ListPreferences 创建下拉列表。为此,首先在 res/values 文件夹中创建一个名为 Sounds.xml 的字符串数组文件(以下假设有两个选项)。第一个数组将在您的列表首选项中显示为字符串,第二个数组是映射到它的整数值。
列表首选项本身如下所示:
现在我们只需要能够在用户每次选择选项时播放声音。这是通过将 OnPreferenceChangeListener 添加到列表首选项小部件来完成的。 (请参阅下面的 OnPreferenceChangeListener 伪代码)
The best solution that worked for me is:
Use ListPreferences to create a dropdown. For this first create a string array file called sounds.xml in res/values folder (following assumes there are two options). The first array would show up as strings in your list preference and the second is the integer value mapped to it.
The list preference itself would look as follows:
Now we just need to be able to play the sound each time the user selects an option. That is done by adding an OnPreferenceChangeListener to the list preference widget. (see pseudo code below for the OnPreferenceChangeListener )