is it possible to put a mp3 soundfile into my app folder and set this as the default ringtone / notification sound in the settings of my app?
For now I have a settings screen where you can select a ringtone that will be taken when a notification comes up from my app. This works but you can only select sounds existing on the device. What I want is to bring my own ringtone file to the device when installing the app. Is this possible? E.g. store the mp3 into the assets folder and call it there?
EDIT:
MyApp.class:
public class MyApp extends Application {
public static SharedPreferences sp;
@Override
public void onCreate() {
super.onCreate();
// initialize default settings values if not already done (does not overwrite existing settings)
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
sp = PreferenceManager.getDefaultSharedPreferences(this);
}
public static String getRingtone() {
return sp.getString("ringtone_pref", "DEFAULT_NOTIFICATION_URI");
}
public static boolean getVibrate() {
return sp.getBoolean("vibrate_pref", true);
}
}
SettingsFragment.class:
public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
public void SettingFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
getRingtoneTitle();
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
getRingtoneTitle();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePreference(key);
}
private void updatePreference(String key) {
Preference pref = findPreference(key);
if (pref instanceof RingtonePreference) {
Uri ringtoneUri = Uri.parse(KitchenHelper.getRingtone());
Ringtone ringtone = RingtoneManager.getRingtone(this.getActivity(), ringtoneUri);
if (ringtone != null) { pref.setSummary(ringtone.getTitle(this.getActivity())); }
}
}
private void getRingtoneTitle() {
Ringtone ringtone = RingtoneManager.getRingtone(this.getActivity(), Uri.parse(KitchenHelper.getRingtone()));
if (ringtone != null) { findPreference("ringtone_pref").setSummary(ringtone.getTitle(this.getActivity())); }
}
}