简体   繁体   English

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

[英]How can I fetch the Audio files from Android Device above Android 4.0 like Whatsapp?

在此处输入图片说明

I want to show track same as Image and want to play it there. 我想显示与Image相同的曲目,并想在那里播放。

Using below code I can try to fetch Mp3 files but can't play that file there. 使用下面的代码,我可以尝试获取Mp3文件,但不能在那里播放该文件。

So please help me.. 所以请帮帮我..

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

Using this method I can Find the detail of all Mp3 files from whole sdcard. 使用这种方法,我可以从整个sdcard中查找所有Mp3文件的详细信息。 In this Track_Bean is a Get and Set Method of Different String variables. 在此Track_Bean中是不同String变量的Get and Set方法。

For Example 例如

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;

 }

Try this hope it was helpful for you 尝试这个希望对您有所帮助

  1. You have to pass the intent 你必须通过intent

private void getAudio() {
    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i,0011);
}

  1. In onActivityResult you get uri of media onActivityResult您会获得uri的媒体

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

        }
    }
}

Output 产量


在此处输入图片说明

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

相关问题 如何将来自 whatsapp 的音频分享到我的 android 应用程序? - How can I share an audio from whatsapp to my android app? 如何在 android 中将应用音频文件与 whatsapp 音频文件分开 - How to separate app audio files from whatsapp audio files in android 如何将音频文件从我的应用程序发送到 Whatsapp? - How can I send audio files from my app to Whatsapp? 如何从Android 4.0及更高版本的远程URL获取视频缩略图? - How can i get thumbnail of video from Remote URL for android 4.0 and above? 如何在Android中发送类似WhatsApp的媒体文件 - How to send Media files like whatsapp in android Android装置(4.0或以上)GPS开启/关闭 - Android device(4.0 or above) GPS turn on/off 如何在Android 4.0设备上以编程方式启用USB网络共享? - How I can enable USB Tethering programmatically on an Android 4.0 device? 我们如何才能成为单个音频文件的一部分。 就像我想从Android中的一个音频文件制作两个音频文件一样。 - How can we make part of single audio file. like i want to make two audio files from one audio file in Android. 如何从android上的usb设备读取文件? - How can I read files from usb device on android? 如何将 WhatsApp 视频文件夹中的视频文件上传到 android 设备上的 Firebase? - How to upload video files from WhatsApp Video Folder to Firebase on android device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM