拿起Android音频文件

拿起Android音频文件

本文介绍了拿起Android音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从SD卡中获取音频文件并播放.我认为可以通过获取音频文件的URI来完成.因此,要选择一个音频文件,我正在使用以下代码:

I need to fetch an audio file from SD Card and play it. I think this can be done by getting URI of an audio file. So, to pick an audio file I'm using following code:

Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Audio "), reqCode);

现在,我可以浏览音频文件并选择其中之一.

Now, I can browse for audio files and select one of them.

问题::如何在我的onActivityResult中读取所选文件的URI?

QUESTION: How to read the URI of picked file in my onActivityResult?

推荐答案

当您要选择音频时,可以在项目中放置以下代码.

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

并在同一Activity中覆盖onActivityResult,如下所示

And override onActivityResult in the same Activity, as below

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

这篇关于拿起Android音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:46