- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Android 中使用 MediaRecorder 来录制视频,mu 当前参数是:
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoEncodingBitRate(2500000);
mMediaRecorder.setVideoSize(640, 480);
我尝试过的所有设备(4.0 及以上)都支持这些参数,但是如果我将代码的最后一行更改为:
mMediaRecorder.setVideoSize(960, 540);
然后出现错误:
java.lang.RuntimeException: start failed.
android.media.MediaRecorder.start(Native Method)
谁能帮忙,因为我想以 960x540 分辨率录制视频。
最佳答案
使用这段代码会有帮助:
try {
recording = true;
mrec = new MediaRecorder();
mCamera.unlock();
mrec.setCamera(mCamera);
//Set audio source
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
//set video source
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//set output format
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
int width = 0;//set whatever
int height = 0;//set whatever
try {
//get the available sizes of the video
List<Size> tmpList = getSupportedVideoSizes();
final List<Size> sizeList = new Vector<Size>();
// compare the apsect ratio of the candidate sizes against the
// real ratio
Double aspectRatio = (Double.valueOf(getWindowManager()
.getDefaultDisplay().getHeight()) / getWindowManager()
.getDefaultDisplay().getWidth());
for (int i = tmpList.size() - 1; i > 0; i--) {
Double tmpRatio = Double.valueOf(tmpList.get(i).height)
/ tmpList.get(i).width;
if (EnableLog.LOG_TAG) {
Log.e("Width & height", tmpList.get(i).width + " x "
+ tmpList.get(i).height);
}
if (Math.abs(aspectRatio - tmpRatio) < .15) {
width = tmpList.get(i).width;
height = tmpList.get(i).height;
sizeList.add(tmpList.get(i));
}
}
} catch (Exception e) {
e.printStackTrace();
}
// set the size of video.
// If the size is not applicable then throw the media recorder stop
// -19 error
mrec.setVideoSize(width, height);
// Set the video encoding bit rate this changes for the high, low.
// medium quality devices
mrec.setVideoEncodingBitRate(1700000);
//Set the video frame rate
mrec.setVideoFrameRate(30);
//set audio encoder format
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
//set video encoder format
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//Show the display preview
mrec.setPreviewDisplay(surfaceHolder.getSurface());
//output file path
mrec.setOutputFile(output_path);
mrec.prepare();
mrec.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
还有这个:
public List<Size> getSupportedVideoSizes() {
if (params.getSupportedVideoSizes() != null) {
return params.getSupportedVideoSizes();
} else {
// Video sizes may be null, which indicates that all the supported
// preview sizes are supported for video recording.
return params.getSupportedPreviewSizes();
}
}
希望这能解决您的问题。
注意
更新 4.4.2 后,getPreviewsizes() 在 Samsung Galaxy Tab 3(7") 中不起作用(对我而言)。所以我希望预览尺寸不适用于所有设备。因此,请先检查视频尺寸,如果返回尺寸,则使用它,或者如果它返回 null,则使用我的代码中的 getPreviewsizes。
我想您收到了另一条错误消息,例如媒体记录器启动失败错误 -19?是吗?
关于java - 无法在 Android MediaRecorder 中设置手动视频大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058579/
我正在为 chrome 商店构建一个屏幕录像机插件。我是 将麦克风的音轨添加到媒体流 包含( 屏幕的视频轨道 + 系统音频轨道 )。所以最终流包含 2 个音轨,一个是麦克风,另一个是系统音频。 当我将
当我尝试在我的程序中制作摄像机时,出现此错误: E/MediaRecorder﹕ start failed: -19 代码是 try { final SurfaceView sv = (Sur
const [rec, setRec] = useState({}); const [onRec, setOnRec] = useState(true); useEffect(() => {
这段代码在 Debug模式下工作得很好,但当不是 Debug模式时它总是抛出运行时异常。 mMediaRecorder.stop(); 根据 Java 文档: Stops recordin
当我尝试为我的 MediaRecorder 设置视频大小时,我在启动方法中收到 RuntimeException。 mRecorder.setAudioSource(MediaRecorder.Aud
录制音频是一个很长的操作,所以我启动mRecorder?.start()在服务内的协程中,您可以看到 RecordService.kt。 我调用 suspend fun startRecord(){.
使用媒体记录器,我可以在 azure 上上传和附加视频 blob。但是无法使用以下代码在下载时查找组合视频 - var chunks =[]; var mediaRecorder = new Medi
我在我的应用程序中集成了摄像头。当用户单击捕获按钮时,我隐藏了工具栏,以便摄像头预览屏幕尺寸增加。这会导致应用程序在停止在线录制时崩溃 - mMediaRecorder.stop(); 。 java.
我想在按住按钮时使用 MediaRecorder 开始录制语音消息。尝试在 onLongClickListener 中开始录制时出现 IllegalStateException。 如堆栈跟踪中所述,我
我的应用需要录制最长 8 秒的视频。这已经通过 MediaRecorder.setMaxDuration(long milliseconds) 实现。该应用还需要顶部的进度条和带有剩余时间倒计时的标签
使用时 CanvasCaptureMediaStream和 MediaRecorder,有没有办法在每一帧上获取一个事件? 我需要的和 requestAnimationFrame() 没什么不同,但我
我正在尝试使用媒体录音机来录制音频。我让它工作,以便我能够录制和播放。唯一的问题是我无法记录程序第二次崩溃的情况。我有一个单独的方法来重置 MediaRecorder,但无论我将它放在哪里,它都不起作
我正在使用 MediaRecorder 录制视频,但似乎无论我使用什么设置,帧率都令人震惊(~ 1fps) 这是我的代码: recorder.setAudioSource(MediaRecor
如果我使用 MediaRecorder.AudioSource.MIC 声音会正常录制。如果 MediaRecorder.AudioSource.VOICE_DOWNLINK 声音以慢速播放。我需要它
我查看了其他人遇到的这个问题,但没有找到合适的解决方案。像他们一样,我遵循了相机功能教程:http://developer.android.com/guide/topics/media/camera.
我正在准备一个小型 Android 应用程序,用于捕获用户的照片和录音。用户会用他的相机拍照,然后在查看该照片时添加音频评论。 为了捕捉音频,我正在使用 MediaRecorder [因为 andro
我想录制通话语音,但我收到 MediaRecorder:start failed : -2147483648 这是我的通话记录代码块 public void SesKayitBaslat(Str
我的应用程序录制了一个音频 fragment ,并在录制完成后使用 Retrofit2 将 fragment 发送到服务器。服务器接收到文件,但是文件损坏了,我说的损坏是无法播放。我使用以下 URL(
我尝试实现的方法如下所示。 它保存了文件,音频没问题,但视频全是绿色线条。 我做错了什么? camera.unlock(); mediaRecorder = new MediaRecorder();
我首先尝试创建 SurfaceView: SurfaceView sv = new SurfaceView(context); // Get a surface surfaceHolder = sv.
我是一名优秀的程序员,十分优秀!