gpt4 book ai didi

android - 使用 mediaStore 中的 mediaplayer 在 ActivityResult 上播放歌曲

转载 作者:行者123 更新时间:2023-11-30 02:19:58 27 4
gpt4 key购买 nike

我想播放从 MediaStore.Audio 中选择的歌曲。我得到了 URi 并在烘烤后进行了测试。但它没有使用媒体播放器播放。

 Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(intent, 1234);

在 ActivityResult 上

if(requestCode == 1234 && resultCode == RESULT_OK)
{
Uri fortsting = getIntent().getData();

Uri uriSound= data.getData();

Toast.makeText(context, "USound " + uriSound.toString() + " tsting "+ fortsting.toString() , Toast.LENGTH_LONG).show();

//String extra = getRealPathFromURI(uriSound);

try
{
playSong(uriSound);
}
catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//play(extra);
}

我也尝试使用 getRealPath

private String getRealPathFromURI(Uri contentUri) 
{
String[] proj = { MediaStore.Audio.Media.DATA };
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);

if(cursor.getCount()==0 || cursor==null)
return null;

cursor.moveToFirst();
return cursor.getString(column_index);

}

最佳答案

据我了解,问题是,即使您正确获取了 uri,歌曲也没有在媒体播放器中打开?

看看这些线程,可能会有用:Android How to get a single song's ID info from a know URISelect a music file to play with MediaPlayer

也请贴出playSong()方法的内容。它应该是这样的:

private void play(Context context, Uri uri) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(context, uri);
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

关于android - 使用 mediaStore 中的 mediaplayer 在 ActivityResult 上播放歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28720378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com