Android 如何从 sdcard/file 管理器加载音频文件并播放
Android How to Load an Audio file from the sdcard/file manager and play it
我正在开发一个应用程序,在那个应用程序中我有一个按钮,名为 'choose sound'。当用户单击此按钮时,应要求 he/she 从文件 manager/memory.
中选择任何音频文件
所以,我知道为此,我必须使用 Intent.Action_GetData
。我也在做同样的事情:
//code start
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,1);
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == 1){
if(resultCode == RESULT_OK){
//the selected audio.
Uri uri = data.getData();
int SoundID=soundPool.Load(uri.toString(), 1);
//SoundPool is already constructed and is working perfectly for the resource files
PlaySound(SoundID);
//PlaySound method is already defined
}
}
super.onActivityResult(requestCode, resultCode, data);
}
//end of code
但它不起作用
现在,在 OnActivityResult
中,我不知道如何加载用户选择的文件的正确 URI,因为在 Android 4.4
之前,它 returns 是不同的 URI,在 Android 4.4
之后=] 它 returns intent.GetData();
上的不同 URI。现在,我必须做什么?
此外,我知道要播放音频文件,我必须使用 SoundPool
,而且我也有相应的代码,事实上它对 resource/raw/audio 文件工作正常,但是如何从这个 URI
到 SoundPool
中的 load/play 个文件?
当你想要select音频时,你可以在你的项目中加入下面的代码。
Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload,1);
并覆盖Activity结果相同Activity,如下
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == 1){
if(resultCode == RESULT_OK){
//the selected audio.
Uri uri = data.getData();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
尝试获取路径:
//method to get the file path from uri
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
然后加载它:
s2 = soundPool.load(YOU_PATH, PRIORITY);
在您的 onActivityResult()
中,进行以下更改:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null)
{
String realPath = null;
Uri uriFromPath = null;
realPath = getPathForAudio(YourActivity.this, data.getData());
uriFromPath = Uri.fromFile(new File(realPath)); // use this uriFromPath for further operations
}
}
将此方法添加到您的 Activity
:
public static String getPathForAudio(Context context, Uri uri)
{
String result = null;
Cursor cursor = null;
try {
String[] proj = { MediaStore.Audio.Media.DATA };
cursor = context.getContentResolver().query(uri, proj, null, null, null);
if (cursor == null) {
result = uri.getPath();
} else {
cursor.moveToFirst();
int column_index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
result = cursor.getString(column_index);
cursor.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (cursor != null) {
cursor.close();
}
}
return result;
}
希望它能完成你的工作。您可以使用 MediaPlayer
class 也可以播放音频
我正在开发一个应用程序,在那个应用程序中我有一个按钮,名为 'choose sound'。当用户单击此按钮时,应要求 he/she 从文件 manager/memory.
中选择任何音频文件所以,我知道为此,我必须使用 Intent.Action_GetData
。我也在做同样的事情:
//code start
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,1);
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == 1){
if(resultCode == RESULT_OK){
//the selected audio.
Uri uri = data.getData();
int SoundID=soundPool.Load(uri.toString(), 1);
//SoundPool is already constructed and is working perfectly for the resource files
PlaySound(SoundID);
//PlaySound method is already defined
}
}
super.onActivityResult(requestCode, resultCode, data);
}
//end of code
但它不起作用
现在,在 OnActivityResult
中,我不知道如何加载用户选择的文件的正确 URI,因为在 Android 4.4
之前,它 returns 是不同的 URI,在 Android 4.4
之后=] 它 returns intent.GetData();
上的不同 URI。现在,我必须做什么?
此外,我知道要播放音频文件,我必须使用 SoundPool
,而且我也有相应的代码,事实上它对 resource/raw/audio 文件工作正常,但是如何从这个 URI
到 SoundPool
中的 load/play 个文件?
当你想要select音频时,你可以在你的项目中加入下面的代码。
Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload,1);
并覆盖Activity结果相同Activity,如下
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == 1){
if(resultCode == RESULT_OK){
//the selected audio.
Uri uri = data.getData();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
尝试获取路径:
//method to get the file path from uri
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
然后加载它:
s2 = soundPool.load(YOU_PATH, PRIORITY);
在您的 onActivityResult()
中,进行以下更改:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null)
{
String realPath = null;
Uri uriFromPath = null;
realPath = getPathForAudio(YourActivity.this, data.getData());
uriFromPath = Uri.fromFile(new File(realPath)); // use this uriFromPath for further operations
}
}
将此方法添加到您的 Activity
:
public static String getPathForAudio(Context context, Uri uri)
{
String result = null;
Cursor cursor = null;
try {
String[] proj = { MediaStore.Audio.Media.DATA };
cursor = context.getContentResolver().query(uri, proj, null, null, null);
if (cursor == null) {
result = uri.getPath();
} else {
cursor.moveToFirst();
int column_index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
result = cursor.getString(column_index);
cursor.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (cursor != null) {
cursor.close();
}
}
return result;
}
希望它能完成你的工作。您可以使用 MediaPlayer
class 也可以播放音频