[英]Load Ringtone into Mediaplayer from SharedPreferences
I'm afraid I have some sort of fundamental misunderstanding of how URI works here. 恐怕我对URI在这里的工作方式有一些根本的误解。 I am trying to save this alarm tone to my SharedPreferences file and then restore it in the same way. 我试图将此警报音保存到我的SharedPreferences文件中,然后以相同的方式还原它。
I believe the problem is how I'm parsing the Uri, I'm not particularly aware of how URI would be retrieved 我认为问题在于我如何解析Uri,我不太了解如何检索URI。
I have tried storing the following into my sharedpreferences. 我尝试将以下内容存储到我的sharedpreferences中。
//the displayed name of the ringtone
RingtoneManager.getRingtone(this, uri).getTitle(this)
and 和
data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
The string of each restores properly, but I'm not aware of the actual key I need to parse to retrieve the alarm tone that I'm looking for. 每个字符串都可以正确还原,但是我不知道我需要解析实际的密钥以检索所需的警报音。
Getting preferences as follows 获取首选项如下
/** Restore alarm tone and update UI */
if (mSettings.contains(ALARM_TONE)){
alarmTone = mSettings.getString(ALARM_TONE, null);
if (alarmTone != null) {
uri = Uri.parse(alarmTone);
TextView t = (TextView) findViewById(R.id.alarmTone);
t.setText(RingtoneManager.getRingtone(this, uri).getTitle(this));
mp = MediaPlayer.create(getApplicationContext(), uri);
}
}
The solution I found uses Uri.toString()
to save the Uri to preferences. 我找到的解决方案使用Uri.toString()
将Uri保存到首选项。 Loading the ringtone back using Uri.parse(preferenceString)
使用Uri.parse(preferenceString)
重新加载铃声
onActivityresult, I immediately save the uri string to my preferences onActivityresult,我立即将uri字符串保存为我的首选项
/** For selecting an alarmtone */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ALARM_URI:
uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
TextView mTextView = (TextView) findViewById(R.id.alarmTone);
mTextView.setText(RingtoneManager.getRingtone(this, uri).getTitle(this));
mp = new MediaPlayer();
mp = MediaPlayer.create(getApplicationContext(), uri);
mp.setLooping(true);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(ALARM_TONE, uri.toString());
editor.commit();
break;
default:
break;
}
}
}
Load the URI on start here 从此处开始加载URI
if (mSettings.contains(ALARM_TONE)){
alarmTone = mSettings.getString(ALARM_TONE, null);
if (alarmTone != null) {
uri = Uri.parse(alarmTone);
//update textview to loaded alarm tone
TextView t = (TextView) findViewById(R.id.alarmTone);
t.setText(RingtoneManager.getRingtone(this, uri).getTitle(this));
mp = MediaPlayer.create(getApplicationContext(), uri);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.