录制时如何在MediaRecorder中设置MIME_TYPE?

时间:2015-01-07 06:43:05

标签: android

我想录制视频并将MIME_TYPE设置为video/mp4

private void startRecord() throws IllegalStateException, IOException {
    mCamera.unlock();
    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setPreviewDisplay(mSurface.getHolder().getSurface());
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setOrientationHint(90);

    // ----------------- error code
    // mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    // mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    // mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    // ---------------------

    if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_1080P)) {
        mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));
    } else {
        mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    }
    outputFile = new File(AppContext.geTempFileDir(), random() + "temp.mp4");
    filePath = outputFile.getCanonicalPath();
    mMediaRecorder.setOutputFile(outputFile.getCanonicalPath());
    mMediaRecorder.prepare();
    mMediaRecorder.start();
    cdt.start();
}

我尝试error code设置MIME_TYPE。但是我得到了例外IllegalStateException。所以我想问两个问题:

  1. 如何使用MIME_TYPE重新编码时设置MediaRecorder
  2. 为什么我会例外?

1 个答案:

答案 0 :(得分:0)

您可以在保存录制文件的同时设置MIME类型,共享一些代码 -

public class Main extends Activity {
    private MediaRecorder mediaRecorder;
    private File file = null;
    static final String PREFIX = "record";
    static final String EXTENSION = ".3gpp";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mediaRecorder = new MediaRecorder();

        Button startRecording = (Button) findViewById(R.id.startBtn);
        Button stopRecording = (Button) findViewById(R.id.stopBtn);
        startRecording.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    startRecording();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        stopRecording.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                stopRecording();
                saveToDB();
            }
        });
    }

    /**
     * This method starts recording process
     * @throws Exception
     */
    private void startRecording() throws Exception {
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        if (file == null) {
            File rootDir = Environment.getExternalStorageDirectory();
            file = File.createTempFile(PREFIX, EXTENSION, rootDir);
        }
        mediaRecorder.setOutputFile(file.getAbsolutePath());
        mediaRecorder.prepare();
        mediaRecorder.start();
    }

    /**
     * This method stops recording
     */
    private void stopRecording() {
        mediaRecorder.stop();
        mediaRecorder.release();
    }

    /**
     * This method sets all metadata for audio file
     */
    private void saveToDB() {
        ContentValues values = new ContentValues(3);
        long current = System.currentTimeMillis();
        values.put(MediaColumns.TITLE, "My Audio record");
        values.put(MediaColumns.DATE_ADDED, (int) (current / 1000));
        values.put(MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaColumns.DATA, file.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));
    }
}

希望这会有所帮助:)