如何将MP3音乐(String
格式)解析为Uri并使用RingtoneManager
播放?有什么建议
编辑:我尝试了以下类似操作,其中 item1 的格式为String
。但是当我运行该应用程序时,音乐无法播放。
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media.TITLE,
};
Cursor cursor = this.managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
spin.setOnItemSelectedListener(this);
List<String> songs = new ArrayList<String>();
while (cursor.moveToNext()) {
songs.add(cursor.getString(0));
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, songs);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(dataAdapter);
.
.
.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
item1 = parent.getItemAtPosition(position).toString();
}
.
.
Uri par=Uri.parse(item1);
Uri alarmSound = RingtoneManager.setActualDefaultRingtoneUri(context,RingtoneManager.TYPE_ALARM,par);
Ringtone ringtone = RingtoneManager.getRingtone(context,par);
.
.
.
ringtone.play();
答案 0 :(得分:3)
如果它是有效的Uri,请使用Uri.parse()
答案 1 :(得分:1)
您可以使用
/**
* Creates a Uri which parses the given encoded URI string.
*
* @param uriString an RFC 2396-compliant, encoded URI
* @throws NullPointerException if uriString is null
* @return Uri for this given uri string
*/
public static Uri parse(String uriString) {
return new StringUri(uriString);
}