我想录制视频并将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
。所以我想问两个问题:
MIME_TYPE
重新编码时设置MediaRecorder
?答案 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));
}
}
希望这会有所帮助:)