如何从Android 4.0以上的Android设备中获取音频文件,如Whatsapp?

时间:2014-11-07 07:14:03

标签: android audio mp3

enter image description here

我希望显示与Image相同的曲目,并希望在那里播放。

使用下面的代码我可以尝试获取Mp3文件,但不能在那里播放该文件。

所以请帮助我..

     public void openGalleryAudio(){
      Intent intent = new Intent();
      intent.setType("audio/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      startActivityForResult(Intent.createChooser(intent,"Select Audio "), SELECT_AUDIO);
     }

2 个答案:

答案 0 :(得分:0)

使用此方法我可以从整个SD卡中查找所有Mp3文件的详细信息。  在这个Track_Bean中是一个不同字符串变量的Get和Set方法。

例如

public class Track_Bean {
    private String mTitle = "";

public String getmTitle() {
    return mTitle;
}

public void setmTitle(String mTitle) {
    this.mTitle = mTitle;
 }
}

================================ ****** ====== =================

 /**
* SD CARD QUERIES
*/
public ArrayList<Track_Bean> getAllSdCardTrack_Beans(Context context) {
ArrayList<Track_Bean> Track_Beans = new ArrayList<Track_Bean>();

    Cursor c = context
            .getContentResolver()
            .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[] { MediaStore.Audio.Media._ID,
                            MediaStore.Audio.Media.DATA,
                            MediaStore.Audio.Media.ARTIST,
                            MediaStore.Audio.Media.ALBUM,
                            MediaStore.Audio.Media.DURATION,
                            MediaStore.Audio.Media.DISPLAY_NAME,
                            MediaStore.Audio.Media.ALBUM_ID }, "1=1",
                    null, null);
    if (c.moveToFirst()) {
        do {
            Track_Bean mTcBean = new Track_Bean();


             String mTitle = c
                     .getString(c
                             .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));

             if(mTitle.trim().endsWith(".mp3") || mTitle.trim().endsWith(".MP3")){
                 mTcBean.setmTitle(mTitle);

                 String mArtist = c
                         .getString(c
                                 .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                 mTcBean.setmArtist(mArtist);

                 //Media Id
                 String mId = c.getString(c
                         .getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                 mTcBean.setmId(mId);
                 //ALbumName
                 String mAlbumName = c
                         .getString(c
                                 .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
                 mTcBean.setmAlbumName(mAlbumName);

                 String mAlbumID = c
                         .getString(c
                                 .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                 mTcBean.setmAlbumId(mAlbumID);
                 //Path
                 String mPath = c
                         .getString(c
                                 .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                 mTcBean.setmPath(mPath);
                 System.out.println("PATH"+mPath);
                //Duration
                 long mDuration = c
                         .getLong(c
                                 .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                 String mFormattedDuration = DateUtils
                         .formatElapsedTime(mDuration / 1000);
                 mTcBean.setmDuration(mFormattedDuration);

                 Track_Beans.add(mTcBean);
             }else{
                 System.out.println("The Track ext is not Mp3::"+mTitle);
             }

        } while (c.moveToNext());
        if (c != null)
            c.close();
    }
    System.out.println("Size of array::"+Track_Beans.size());
    mLv_tracks.setAdapter(new Audio_Post_Adapter(Audio_Fetch.this, Track_Beans));
return Track_Beans;

 }

答案 1 :(得分:0)

试试这个希望它对你有帮助

  1. 您必须通过intent
  2. private void getAudio() {
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i,0011);
    }
    
    1. onActivityResult中,您获得uri媒体
    2. @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
      
          if (requestCode == 0011 && resultCode == Activity.RESULT_OK){
              if ((data != null) && (data.getData() != null)){
                  Uri audioFileUri = data.getData();
                  Log.e(TAG, "onActivityResult: 232"+audioFileUri);
      
              }
          }
      }
      

      <强>输出

      enter image description here