我试图长时间拍摄视频。
以下是初始化媒体记录器的代码。
export default class SomeCard extends React.Component {
constructor(){
super();
this.state = {
data_source: []
}
}
componentDidMount(){
const {name, data} = this.props.data;
if(name == 'AAA'){
let values_a = data.map((tmp_data) =>{
let date = new Date(tmp_data.date);
return {
x: date,
open: tmp_data.open,
high: tmp_data.high,
low: tmp_data.low,
close: tmp_data.close
};
});
let final_tmp_data = [
{
name: name,
values: values_a
}
];
console.log(final_tmp_data); //NOT EMPTY
this.setState({data_source: final_tmp_data});
console.log(data_source); //NOT EMPTY
}else if(name == 'BBB'){
let values_b = data.map((tmp_data) => {
let date = new Date(tmp_data.date);
let tmp_date = date.getFullYear();
return {
"x": String(tmp_date),
"y": tmp_data.value
};
});
let final_cash_data = [
{
"name": name,
"values": values_b
}
];
console.log(final_cash_data); //NOT EMPTY
this.setState({data_source: final_cash_data});
console.log(this.state.data_source); //EMPTY!!!!!!!!
}
.....
}
此方法从onSurfaceCreated方法调用,如下所示:
private void initRecorder(Surface surface) {
if (camera == null) {
camera = Camera.open();
camera.unlock();
}
if (mediaRecorder == null)
mediaRecorder = new MediaRecorder();
mediaRecorder.setPreviewDisplay(surface);
mediaRecorder.setCamera(camera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
File file_image = getDirectory();
if (!file_image.exists() && !file_image.mkdirs()) {
Toast.makeText(CameraActivity.this, "Cannot Create Directory", Toast.LENGTH_SHORT).show();
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddhhmmss");
String date = sdf.format(new Date());
String photoFile = "GIFVideo" + date + ".mp4";
String file_name = file_image.getAbsolutePath() + "/" + photoFile;
File picFile = new File(file_name);
mediaRecorder.setOutputFile(file_name);
// No limit. Don't forget to check the space on disk.
mediaRecorder.setMaxDuration(-1);
mediaRecorder.setVideoFrameRate(15);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
// This is thrown if the previous calls are not called with the
// proper order
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
initSuccess = true;
}
给我错误的代码是:
@Override
public void surfaceCreated(SurfaceHolder holder) {
//Open Camera
try {
camera = Camera.open();
} catch (RuntimeException e) {
e.printStackTrace();
}
if (!initSuccess) {
initRecorder(holder.getSurface());
}
setCameraParameters("back");
try {
//Camera Started, tell where to draw
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
以下是我遇到的错误:
btnCapture.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (!recording) {
mediaRecorder.start();
Toast.makeText(CameraActivity.this, "Video Recording Started", Toast.LENGTH_SHORT).show();
recording = true;
}
return false;
}
});