我将我的旧nexus设备升级到最新版本,现在MediaPlayer将不再播放我通过ringtoneManager选择器检索的自定义铃声路径。
我有两个媒体作为例子:
和
我试图在android中同时使用mediaplayer API。两个媒体路径都由RingtoneManager返回给我,如下所示:
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
// activity stack history, its a one time deal only
// intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
"Please Select A Ringtone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
//intent.putExtra(RingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM, true);
try {// 44 arbitrary number to recognize our intent
startActivityForResult(intent, 44);
}
//etc这是我如何检索选定的铃声。除了用户从列表中选择自定义铃声外,一切正常工作,我得到一个mediaplayer IO异常,无法播放数据资源。
// find out what ringtone the user selected and play the tone.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Ringtone ringtone;
if (requestCode == 44 && resultCode == RESULT_OK) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);}
// uri被检索,其值为:内容://media/内部/音频/media/86
以下是我尝试播放这个自定义铃声时收到的mediaplayer错误:
e = {java.io.IOException@4499} "java.io.IOException: setDataSource failed.: status=0x80000000"
cause = {java.io.IOException@4499} "java.io.IOException: setDataSource failed.: status=0x80000000"
detailMessage = {java.lang.String@4503} "setDataSource failed.: status=0x80000000"任何其他媒体都可以正常工作,而不是自定义铃声。
发布于 2015-07-05 19:38:12
看起来路径是不被接受的,我必须使用完整的路径来达到自定义铃声,因为某些原因。谷歌,有什么理由吗?这是我的解决办法。检查uri在isValidUri中是否有效。如果它无效,我们调用getRingtonePathFromContentUri来获取资源的目录路径,然后使用它。据报道,这一问题是here
/**
* checks if a url such as content://media/internal/audio/media/86 can be played.
* if not returns no and we can fall back to something else.
**/
public boolean isValidUri(String contentUri){
boolean result=true;
MediaPlayer player = new MediaPlayer();
try {
player.setDataSource(contentUri);
} catch (IOException e) {
e.printStackTrace();
result = false;
}
return result;
}
//gets the SD card path for a ringtone uri
public String getRingtonePathFromContentUri(Context context,
Uri contentUri) {
String[] proj = { MediaStore.Audio.Media.DATA };
Cursor ringtoneCursor = context.getContentResolver().query(contentUri,
proj, null, null, null);
ringtoneCursor.moveToFirst();
String path = ringtoneCursor.getString(ringtoneCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
ringtoneCursor.close();
return path;
}发布于 2016-05-31 05:49:27
您可以将mp3文件放在res/raw文件夹中:
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.myringtone);
mediaPlayer.start();https://stackoverflow.com/questions/31233606
复制相似问题