AudioRecord无法在2.3上保存文件

时间:2012-08-09 08:39:51

标签: android audio

我的项目开发平台是google API7。它可以使用以下版本的2.2运行设备但是当我在上面的2.3版本上安装时,我的文件无法保存在SD卡中

这是我的代码

class ButtonListener implements OnClickListener
{
    public void onClick(View v)
    {
        if (v == startButton) 
        {
        setTitle("Start Recording and transmitting the file");  
        bar.setMax(second);
        updateBarHandler.post(updateThread);
        startButton.setVisibility(View.GONE);
        exitButton.setVisibility(View.GONE);
        stopButton.setVisibility(View.GONE);


        }

        if (v == stopButton)                
        {
            setTitle("Stop");
            handler.removeCallbacks(updateThread);  
            recorderInstance.setRecording(false);
            recorderInstance = null;
            startButton.setVisibility(View.VISIBLE);
            exitButton.setVisibility(View.VISIBLE);
        }

        if (v == exitButton) 
        {
            Intent intent = new Intent();
            intent.setClass(MyRecorder.this, User.class);
            startActivity(intent);
            MyRecorder.this.finish();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用此代码录制音频。

File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("Recording", ".3gp", sampleDir);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(audiofile.getAbsolutePath());
    recorder.prepare();
recorder.start();  
    }
    catch (IOException e)
   {
   Log.e(TAG, "sdcard access error");
   return;
}

您可以使用以下代码将此音频保存在SD卡中。

audioFileName=input.getText().toString();
current = System.currentTimeMillis();
String artist = "" + getResources().getText(R.string.artistName);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, audiofile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, audioFileName);
values.put(MediaStore.Audio.Media.DATE_ADDED,  current );
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.ARTIST, artist);              
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ConntentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri1 = contentResolver.insert(base, values);