2012-10-02 74 views
0

Im试图做一些简单的事情,录制音频和视频,无需预览。Android - 准备媒体录制器失败

这里是我的代码:

MediaRecorder mediaRecorder = new MediaRecorder(); 

// Configure the input sources 
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 

// Set the output format 
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); 

// Specify the audio and video encoding   
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 

    //Specify the outputfile 
    mediaRecorder.setOutputFile("/sdcard/Video/myoutputfile.mp4"); 

mediaRecorder.start(); 

In the AndroidManisfest.xml: 
<user-permission android:name="android.permission.RECORD_AUDIO" /> 
<user-permission android:name="android.permission.RECORD_VIDEO" /> 

这将停止应用程序时,它启动。

任何人有任何想法?

+0

不要忘记<使用的许可机器人:名称=“android.permission.WRITE_EXTERNAL_STORAGE” /> – mario

+0

错字:“用户许可”应该是“使用的许可”。 :-) – darrenp

回答

0

尝试在开始记录前调用此函数;

mediaRecorder.prepare(); 

另外,记住你缺少一些必要的“特色”,你必须一套准备调用之前。 这里似乎是你缺少的一些方法;

setPreviewDisplay(); 
setMaxDuration((int) #); 
setOnInfoListener(); 
setVideoSize(#, #); 
setVideoFrameRate(15); 
+0

我再次尝试,并得到相同的错误,应用程序崩溃之前开始! –

+1

有没有关于我在Galaxy Nexus中测试这个事实的信息? Android 4.1.1。我读过这段代码在android 2.3或更高版本中不起作用。 –

+0

你能编辑你的堆栈跟踪吗?我在Galaxy Nexus上使用了一些奇怪的设置,但我确实记得我的星系连线崩溃了与MediaRecorder类的很多! (大惊喜,因为它应该是一个谷歌支持的手机使用谷歌api's?哈哈) 使用我的“特殊”设置从来没有为我工作,因为显然它只会在我的联系,然后其他手机会有问题: ( –