7.3 将音频插入MediaStore

    可以将录制的音频放入MediaStore内容提供器,从而可将其用于其他应用程序。这非常类似于之前将图像添加到MediaStore的过程。不过在当前情况下,将在创建之后添加他们。

    我们将创建一个ContentValues对象来保存数据,同时将他们插入到MediaStore。ContentValues对象由一系列的键/值对组成,其中可以使用的键定义为MediaStore.Audio.Media,它是唯一必须的键/值对。

    为了实际插入到MediaStore中,可以使用ContentResolver对象上的insert方法,同时以指向SD卡上音频文件表的Uri,以及包含数据的ContentValues对象作为参数。Uri定义为MediaStore.Audio.Media上的一个常量EXTERNAL_CONTENT_URI。

    以下是一个代码片段,可以插入到CustomRecorder示例中,放在MediaRecorder上的release方法调用(recorder.release())之后。这将把录音插入到MediaStore,并将它提供给其他使用MediaStore查找音频进行播放的应用程序。

1             ContentValues contentValues=new ContentValues();
2             contentValues.put(MediaStore.MediaColumns.TITLE, "this is not music");
3             contentValues.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis());
4             contentValues.put(MediaStore.MediaColumns.DATA,audioFile.getAbsolutePath());
5    
6             Uri newUri=getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues);
原文地址:https://www.cnblogs.com/ZSS-Android/p/3948728.html