如题,照着网上的做的,MediaaRecorder.prepare()执行不下去了,程序也没有报错,求帮助
这里是代码:
1).Activity
package com.example.videorecording;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
// 获取当前时间作为文件名
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
String date = dateFormat.format(now);
// 系统的视频文件
File videoFile;
MediaRecorder mRecorder;
// 记录是否正在进行录制
private boolean isRecording = false;
// layout中的3个
SurfaceView surfaceView = null;
Button start = null;
Button stop = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video_recording);
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
start = (Button) findViewById(R.id.start_button);
stop = (Button) findViewById(R.id.stop_button);
stop.setEnabled(false);
Log.d("ZH", date);
start.setOnClickListener(this);
stop.setOnClickListener(this);
// 设置分辨率
surfaceView.getHolder().setFixedSize(1280, 720);
// 设置该组件让屏幕不会自动关闭
surfaceView.getHolder().setKeepScreenOn(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("ZH", v.getId() +"age");
switch (v.getId()) {
case R.id.start_button:
if (!Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "SD卡不存在,请插入SD卡!",Toast.LENGTH_SHORT).show();
return;
}
try {
videoFile = new File(Environment.getExternalStorageDirectory()
.getCanonicalFile() + "/" + date +".3gp");
Log.d("ZH", v.getId() +"last");
mRecorder = new MediaRecorder();
mRecorder.reset();
//音频录制
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//图像录制
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//设置输出格式(输出格式必须在设置声音编码格式、图像编码格式之前设置)
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置声音编码格式
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置图像编码格式
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//设置分辨率
mRecorder.setVideoSize(1280, 720);
//每秒 4帧
mRecorder.setVideoFrameRate(20);
mRecorder.setVideoEncodingBitRate(8*1024*1024);
mRecorder.setOutputFile(videoFile.getAbsolutePath());
Log.d("ZH", v.getId() + "先");
// 指定使用SurfaceView来预览视频
mRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
Log.d("ZH", v.getId() + "然后");
mRecorder.prepare();
Log.d("ZH", v.getId() + "最后");
//开始录制
mRecorder.start();
//改变按钮
start.setEnabled(false);
stop.setEnabled(true);
isRecording = true;
Log.d("ZH", v.getId() + "aa");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.stop_button:
if(isRecording){
mRecorder.stop();
// 释放资源
mRecorder.release();
mRecorder = null;
//改变按钮
start.setEnabled(true);
stop.setEnabled(true);
}
break;
default:
break;
}
}
}
2).布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_weight="1"
android:id="@+id/surfaceview"/>
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始录制" />
<Button
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止录制" />
</LinearLayout>
3).AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videorecording"
android:versionCode="1"
android:versionName="1.0" >
<!-- 授予该程序录制声音的权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<!-- 授予该程序使用摄像头的权限 -->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 授予使用外部存储器的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.videorecording.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
------解决思路----------------------
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");改成
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
注释掉设置分辨率和帧数
mRecorder.setVideoSize(1280, 720);
mRecorder.setVideoFrameRate(20);