I have sounds in my /raw folder and I would like my user to be able to choose one sound in preferences exactly like RingtonePreference does but only with my sounds.
相关问题
- How can I create this custom Bottom Navigation on
- Can we recover audio from MFCC coefficients?
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Is there a way to play audio on a mobile browser w
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
So finally I looked into the source code of ListPreference and made the same with some modifcations. As I can't use com.android.internal.R.styleable.ListPreference I had to create my own styleable in attrs.xml :
and then import it in my preferences.xml file like this:
and uses it :
In my class CustomSoundListPreference I modified the method onPrepareDialogBuilder to play my sound on item clicked.
Here my RingtonePreference replacement. All system ringtones and your custom ringtones (defined in xml, stored in res/raw) are listed:
ExtraRingtonePreference.java
res/values/attrs.xml
res/values/strings.xml
res/raw
res/xml/preferences.xml
Here is the full code for a custom ListPreference just like a ringtone preference:
Hope this will be helpful to someone.
when making a ringtone preference customization i prefer that the ringtone only play for short while, sort of like a sample sounds. user does not need to hear the entire sound play if they are just choosing a sound from a list. Here is how i achieve this:
first create a service that will play the ringtone (well use ringtone manager to play the sound instead of media player as its handling the cancelling for us):
public class PlayRingtoneService extends Service { static Ringtone r; private Handler handler;
}
also update the manifest with the service:
then using the solutions above but modified you can create a list preference that behaves just like a ringtone preference:
}
now in your xml use it like this:
now to actually load up the values into the list we create a utils method that can get all internal and external media and put it into our model class called songs defined like this:
}
Now in your Util class or just a static method if you want you do make this class which will query the media store for all audio files:
public static List getAllExternalAudioSongs(Context c) { List songList = new ArrayList<>(); ContentResolver contentResolver = c.getContentResolver();
note: the externalPath is just if you want to differentiate internal from external audio files.
Finally, in onCreate of preferenceActivity (or fragment) you can do this:
note: you'll need runtime permissions for external storage. and also to update the summary youll have to do that in the preference activity i believe. anyway this gives a good idea how to play sample audio instead of entire audio file.