我很难在android studio上录制视频。 以下源代码。按下按钮并按下更多时间停止按钮,我可以记录一次而不是两次。
我想录制更多视频。所以我再次点击它再次思考视频记录。 但是app终止没有在android studio上显示任何错误。
我认为我应该重新绘制surfaceview以避免出现任何问题。 记录超过1次的任何想法?
package ica.com.myvideo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{
Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean is_recording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermission();
is_recording=false;
mediaRecorder=new MediaRecorder();
initMediaRecorder();
SurfaceView myVideoView=(SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder=myVideoView.getHolder();
surfaceHolder.addCallback(this);
myButton=(Button)findViewById(R.id.button);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(is_recording){
mediaRecorder.stop();
mediaRecorder.release();
}else{
mediaRecorder.start();
is_recording=true;
}
}
});
}
private void initMediaRecorder() {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ=CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
mediaRecorder.setProfile(camcorderProfile_HQ);
mediaRecorder.setOutputFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()+"/testv.mp4");
Log.e("error",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()+"/testv.mp4");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e("error","surface created");
prepareMediaRecorder();
}
private void prepareMediaRecorder() {
Log.e("error","prepareMediaRecorder");
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
private void checkPermission(){
String[] permssions={
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
};
int permissionCheck= PackageManager.PERMISSION_GRANTED;
for(int i=0; i<permssions.length; i++) {
permissionCheck = ContextCompat.checkSelfPermission(this, permssions[i]);
if (permissionCheck == PackageManager.PERMISSION_DENIED) {
break;
}
}
if(permissionCheck==PackageManager.PERMISSION_GRANTED){
}else{
if(ActivityCompat.shouldShowRequestPermissionRationale(this,permssions[0])){
}else{
ActivityCompat.requestPermissions(this,permssions,1);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==1){
for(int i=0; i<permissions.length; i++){
if(grantResults[i]==PackageManager.PERMISSION_GRANTED){
//
}else{
}
}
}
}
}