录制电话

时间:2015-09-07 12:17:26

标签: android call audio-recording android-mediarecorder

好的,我有一个代码,代码写入外部存储,我希望它写入内部。它给了我错误,首先返回mContext.getFilesDir()。getAbsolutePath()+ path;

第二个错误,说它处于无效状态。

任何?

public class AudioRecorder {

    final MediaRecorder recorder = new MediaRecorder();
    final String path;
    private Context mContext;
    /**
     * Creates a new audio recording at the given path (relative to root of SD card).
     */
    public AudioRecorder(String path, Context context) {
        this.path = sanitizePath(path);
        mContext = context;
    }

    private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (!path.contains(".")) {
            path += ".3gp";
        }
        return mContext.getFilesDir().getAbsolutePath() + path;
    }

    /**
     * Starts a new recording.
     */
    public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
            throw new IOException("SD Card is not mounted.  It is " + state + ".");
        }

        // make sure the directory we plan to store the recording in exists
        File directory = new File(path).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Path to file could not be created.");
        }

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(path);
        recorder.prepare();
        recorder.start();
        Log.i("AudioRecorder" , "Started");
    }

    /**
     * Stops a recording that has been previously started.
     */
    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
        Log.i("AudioRecorder", "Stopped");
    }

}

0 个答案:

没有答案