简体   繁体   English

Android如何从sdcard /文件管理器加载音频文件并播放

[英]Android How to Load an Audio file from the sdcard/file manager and play it

I'm developing an app, and in that app I have one button, named 'choose sound'. 我正在开发一个应用程序,在该应用程序中,我有一个名为“选择声音”的按钮。 When user will click this button, he/she should be asked to choose any audio file from the file manager/memory. 当用户单击此按钮时,应要求他/她从文件管理器/存储器中选择任何音频文件。

So, I know that for this, I'll have to use Intent.Action_GetData . 因此,我知道为此,我必须使用Intent.Action_GetData I'm doing the same: 我在做同样的事情:

//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

but it's not working 但它不起作用
Now, in OnActivityResult , I'm not getting that how to load the proper URI of the file selected by user, because before Android 4.4 , it returns the different URI and after Android 4.4 it returns the different URI on intent.GetData(); 现在,在OnActivityResult ,我不知道如何加载用户选择的文件的正确URI,因为在Android 4.4之前,它将返回不同的URI,而在Android 4.4它将在intent.GetData();上返回不同的URI intent.GetData(); . Now, what I have to do? 现在,我该怎么办?

Also, I know that for playing the audio file, I'll have to use SoundPool , and I have the code for that too, in fact it's working fine for the resource/raw/audio files, but how to load/play files in SoundPool from this URI ? 另外,我知道要播放音频文件,我必须使用SoundPool ,我也有相应的代码,实际上,它对于资源/原始文件/音频文件工作正常,但是如何在其中加载/播放文件来自此URI SoundPool

In your onActivityResult() , do the following changes: 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
    }
}     

Add this method in your Activity : 在您的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;
}

Hope It will do your job. 希望它能做好你的工作。 You can play audio using MediaPlayer class also 您也可以使用MediaPlayer类播放音频

You can put below codes in your project when you want to select audio. 当您要选择音频时,可以在项目中放置以下代码。

Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload,1);

And override onActivityResult in the same Activity, as below 并在同一Activity中覆盖onActivityResult,如下所示

@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);
}

Try to get the path : 尝试获取路径:

 //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;
    }

then load it : 然后加载它:

s2 = soundPool.load(YOU_PATH, PRIORITY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM