Android无法在音乐中找到新创建的声音文件

时间:2016-01-25 05:52:15

标签: android mediarecorder android-mediascanner

我创建了一个文件并用它来保存我的声音文件,但是这个文件无法添加到音乐

File f;
boolean recording=false;
public void startStopRecording(View view){

    try {

        if (!recording) {
            do {
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date();
                String filename = "rec_" + dateFormat.format(date)+".gpp";
                outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myRec/"+filename;
                f = new File(outputFile);
                f.getParentFile().mkdirs();

            }while(f.exists());
            myAudioRecorder = new MediaRecorder();
            myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
            myAudioRecorder.setOutputFile(outputFile);
            myAudioRecorder.prepare();
            myAudioRecorder.start();
            recording=!recording;
        }else{
            myAudioRecorder.stop();
            myAudioRecorder.release();
            myAudioRecorder  = null;
            recording=!recording;

            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(Uri.fromFile(f));
            sendBroadcast(mediaScanIntent);
            MediaScannerConnection.scanFile(this, new String[]{f.getAbsolutePath()}, null, null);
        }
    }  catch (IOException e) {
        e.printStackTrace();
    }

}

我也试过这个,仍然没有用

ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio file");
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, f.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));

如何将我的声音文件添加到音乐?

1 个答案:

答案 0 :(得分:1)

替换

 String filename = "rec_" + dateFormat.format(date)+".gpp";

String filename = "rec_" + dateFormat.format(date)+".3gp";

您可以参考本教程

http://www.tutorialspoint.com/android/android_audio_capture.htm

如果您仍然无法下载此示例

https://github.com/hiteshsahu/Android-Audio-Recorder-Visualization-Master