0

我创建了一个记录音频并将其存储在 SD 卡中的应用程序。我
成功地完成了这部分。这是将音频文件插入 SD 卡的代码。

protected void addRecordingToMediaLibrary() {
    ContentValues values = new ContentValues(4);
    long current = System.currentTimeMillis();
    values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
    values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
    ContentResolver contentResolver = getContentResolver();

    Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Uri newUri = contentResolver.insert(base, values);

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
    Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}

但现在我想在 listView 中显示这个音频文件,但我不知道该怎么做。
请给我任何参考或提示。

4

2 回答 2

0

首先提取所有的mp3文件。

使用我开发的这个方法....获取sd卡的mp3文件

为了获得详细信息,我还问了一个问题Observing changes in android content observer for Audio.Media.EXTERNAL_CONTENT_URI

此代码适用于mnt/sdcradextSdCard

在这段代码中我检索到

  1. 文件名

  2. 文件路径

  3. 时间

  4. 期间

对于每个 mp3 文件。您可以检索很多字段,请参阅此http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html

     private void getAllAudio() {
     Cursor mCursor = null;

     try {
         mCursor = context.getContentResolver().query(
             Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, "_id");

         System.out.println("Cursor count is " + mCursor.getCount());

         if (mCursor.getCount() != 0) {

             do {


                 long date = mCursor.getLong(mCursor
                     .getColumnIndexOrThrow(Audio.Media.DATE_ADDED));

                 String Duration = mCursor.getString(mCursor
                     .getColumnIndexOrThrow(Audio.Media.DURATION));
                 String DISPLAY_NAME = mCursor.getString(mCursor
                     .getColumnIndexOrThrow(Audio.Media.DISPLAY_NAME));
                 String DATA = mCursor.getString(mCursor
                     .getColumnIndexOrThrow(Audio.Media.DATA));
                 double TIME_STAMP = mCursor.getInt(mCursor
                     .getColumnIndexOrThrow(Audio.Media.DATE_ADDED));


                 System.out.println("Name:" + DISPLAY_NAME);
                 System.out.println("data " + DATA);
                 System.out.println("time " + TIME_STAMP);
                 System.out.println("time " + Duration);

                 //Here you can create data base for recoring entries or may can create ArrayList of Class bean object having this fields for showing in ListView.


             } while (mCursor.moveToNext());

         }
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         if (mCursor != null) {
             mCursor.close();
             mCursor = null;
         }
     }

 }
 }
于 2013-05-18T11:07:46.123 回答
0

Try using MediaStore from android.provider.MediaStore package, you can get the music files on the sdcard using this...

And this below tutorial from stackoverflow.com will be a great help...

Android MP3 file Filtering form sdcard

Edit : Removed blog url... Blog no more exits...

于 2012-08-01T10:33:24.993 回答