点击并按住录制视频像藤 [英] Tap and hold to record video like Vine

查看:142
本文介绍了点击并按住录制视频像藤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要作出这样的录像,好像藤的应用程序,坚持记录,释放它停下来,坚持记录和保存,为结束。

I want to make an app that record video, it seems like vine, hold to record, release it stop, hold to record and keep that to the end.

我用MediaRecorder,但它只是一次记录一次,如果我再次开始记录,应用程序是死机了。

I have used MediaRecorder, but it just record once a time, if I start record again, app is crashed.

请告诉我,有没有办法做到这一点?
我编辑我的code:

Please tell me there is any way to do this? I edited my code:

public class VideoRecordingActivity extends AppCompatActivity implements View.OnTouchListener, View.OnLongClickListener {
private Context myContext;
private boolean hasCamera;
private boolean onRecording;

private Camera mCamera;
private CameraPreview mPreview;
private MediaRecorder mediaRecorder;
private boolean cameraFront = false;
private int cameraId;

private int videoNumer;
private boolean isActionDown = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_introduction_recording);
    initUI();
    initialize();
}
private LinearLayout lnCameraPreview;
private ImageButton btn_recording;

private void initUI() {
    lnCameraPreview = (LinearLayout) findViewById(R.id.ln_body_recording);
    btn_recording = (ImageButton) findViewById(R.id.btn_recording);

}

public void initialize() {
    myContext = this;
    mPreview = new CameraPreview(this, cameraId, mCamera);
    lnCameraPreview.addView(mPreview);
    btn_recording.setOnLongClickListener(this);
    btn_recording.setOnTouchListener(this);
    videoNumer = 0;
}
public boolean onLongClick(View v) {
    isActionDown = true;
    try {
        boolean isPrepared = false;
        if (isActionDown)
            isPrepared = prepareMediaRecorder();
        if (isPrepared && isActionDown) {
            // work on UiThread for better performance
            runOnUiThread(new Runnable() {
                public void run() {
                    mediaRecorder.start();
                    onRecording = true;
                }
            });

        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("onLongPress Error ", e.toString());
    }

    return true;
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_UP:
            isActionDown = false;

            try {
                if (onRecording) {

                    if (mediaRecorder != null) {
                        mediaRecorder.stop();
                    }
                    onRecording = false;
                    videoNumer++;
                }
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
    }
    return false;
}



public void onResume() {
    super.onResume();
    if (!hasCamera(myContext)) {
        Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG).show();
        return;
    }
    initCamera();
}

@Override
protected void onPause() {
    super.onPause();
    // when on Pause, release camera in order to be used from other
    // applications
    releaseCamera();
}


private final int cMaxRecordDurationInMs = 30000;
private final long cMaxFileSizeInBytes = 5000000;
private final int cFrameRate = 20;
private File prRecordedFile;

@SuppressLint("SdCardPath")
private boolean prepareMediaRecorder() {
    mediaRecorder = new MediaRecorder();

    try {
        mCamera.unlock();
    } catch (Exception ex) {
        return false;
    }

        // adjust the camera the way you need
        mediaRecorder.setCamera(mCamera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

        //
        CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
        mediaRecorder.setProfile(cpHigh);
        mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    mediaRecorder.setOutputFile("/sdcard/" + videoNumer + "videocapture_example.mp4");

    //set max size
    mediaRecorder.setMaxDuration(600000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M

    try {
        mediaRecorder.prepare();
    } catch (Exception e) {
        releaseMediaRecorder();
        e.printStackTrace();
    }
    return true;
}

private void releaseMediaRecorder() {
    if (mediaRecorder != null) {
        mediaRecorder.reset(); // clear recorder configuration
        mediaRecorder.release(); // release the recorder object
        mediaRecorder = null;
        if (mCamera != null) {
            mCamera.lock(); // lock camera for later use
        }
    }
}

/**
 * Camera
 */

private void initCamera() {
    if (mCamera == null) {
        // if the front facing camera does not exist
        if (findFrontFacingCamera() < 0) {
            Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
        }
        mCamera = Camera.open(findBackFacingCamera());
        mPreview.refreshCamera(mCamera);
    }
    onRecording = false;
}

private boolean hasCamera(Context context) {
    // check if the device has camera
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        hasCamera = true;
    } else {
        hasCamera = false;
    }
    return hasCamera;
}

private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            cameraFront = true;
            break;
        }
    }
    this.cameraId = cameraId;
    return cameraId;
}

private int findBackFacingCamera() {
    int cameraId = -1;
    // Search for the back facing camera
    // get the number of cameras
    int numberOfCameras = Camera.getNumberOfCameras();
    // for every camera check
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            cameraId = i;
            cameraFront = false;
            break;
        }
    }
    this.cameraId = cameraId;
    return cameraId;
}

public void switchCamera() {
    // if the camera preview is the front
    if (cameraFront) {
        int cameraId = findBackFacingCamera();
        if (cameraId >= 0) {
            // open the backFacingCamera
            mCamera = Camera.open(cameraId);
            // refresh the preview
            mPreview.refreshCamera(mCamera);
        }
    } else {
        int cameraId = findFrontFacingCamera();
        if (cameraId >= 0) {
            // open the backFacingCamera
            mCamera = Camera.open(cameraId);
            // refresh the preview
            mPreview.refreshCamera(mCamera);
        }
    }
}

private void releaseCamera() {
    // stop and release camera
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

}

推荐答案

您可以做实现在您的录制按钮设置OnLongClickListener()和OnTouchListener()这个功能。像这样的:

You can do achieve this functionality by setting OnLongClickListener() and OnTouchListener() on your record button. Like this:

recordBtn.setOnLongClickListener(recordBtnLCListener);
recordBtn.setOnTouchListener(recordBtnTouchListener);

然后

@Override
public boolean onLongClick(View v) {
    ivCancel.setVisibility(View.GONE);
    ivDone.setVisibility(View.GONE);
    isActionDown = true;
    try {
        if (isActionDown) {
            initRecorder();
            if (isActionDown)
                prepareRecorder();
        }
        if (isPrepared && isActionDown) {
            mMediaRecorder.start();
            isRecording = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("onLongPress Error ", e.toString());
    }

    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_UP:
            isActionDown = false;

            try {
                if (isRecording) {

                    if (mMediaRecorder != null) {
                        mMediaRecorder.stop();
                    }
                    isRecording = false;
                }
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
    }
    return false;
}

所以,在这样你可以在每次龙$ P $记录video.Means的部分PSS您的录音按钮,录音开始。和释放按钮时,录音停止,在这里你有视频,这部分节省的临时文件夹。
一旦拍完,因为许多影片所有部件,只要你想,那么你必须所有的部分视频制作一个视频结合起来。

So, in this way you can record the parts of video.Means each time you LongPress your record button, the recording starts. And time you release the button, the recording stops and here you have to save this part of video in any temporary folder. Once you done taking all parts of videos as many as you want, then you have to combine all that parts of videos to make a single video.

下面,是code合并保存在文件夹temperory所有的视频​​部分:

Here, is the code to merge all that video parts saved in temperory folder:

public void mergeVideos() {
    try {
        List<Movie> inMovies = new ArrayList<>();
        for (int i = 0; i < videosPathList.size(); i++) {
            String filePath = videosPathList.get(i);
            try {
                Movie movie = MovieCreator.build(filePath);
                if (movie != null)
                    inMovies.add(movie);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        List<Track> videoTracks = new LinkedList<Track>();
        List<Track> audioTracks = new LinkedList<Track>();
        for (Movie m : inMovies) {
            for (Track t : m.getTracks()) {
                try {
                    if (t.getHandler().equals("soun")) {
                        audioTracks.add(t);
                    }
                    if (t.getHandler().equals("vide")) {
                        videoTracks.add(t);
                    }
                } catch (Exception e) {

                }
            }
        }
        Movie result = new Movie();
        if (audioTracks.size() > 0) {
            result.addTrack(new AppendTrack(audioTracks
                    .toArray(new Track[audioTracks.size()])));
        }
        if (videoTracks.size() > 0) {
            result.addTrack(new AppendTrack(videoTracks
                    .toArray(new Track[videoTracks.size()])));
        }
        BasicContainer out = (BasicContainer) new DefaultMp4Builder().build(result);
        File f = null;
        String finalVideoPath;
        try {
            f = setUpVideoFile(Environment
        .getExternalStorageDirectory()+"/MyApp/videos/");
            finalVideoPath = f.getAbsolutePath();

        } catch (IOException e) {
            e.printStackTrace();
            f = null;
            finalVideoPath = null;
        }
        WritableByteChannel fc = new RandomAccessFile(finalVideoPath, "rw").getChannel();
        out.writeContainer(fc);
        fc.close();
        deleteFilesDir(); //In this method you have to delete all parts of video stored in temporary folder.


    } catch (Exception e) {
        e.printStackTrace();
       progressDialog.dismiss();
        finish();
    }
}

File setUpVideoFile(String directory) throws IOException {
    File videoFile = null;
    if (Environment.MEDIA_MOUNTED.equals(Environment
            .getExternalStorageState())) {
        File storageDir = new File(directory);
        if (storageDir != null) {
            if (!storageDir.mkdirs()) {
                if (!storageDir.exists()) {
                    Log.d("CameraSample", "failed to create directory");
                    return null;
                }
            }
        }
        videoFile = File.createTempFile("video_"
                        + System.currentTimeMillis() + "_",
                .mp4, storageDir);
    }
    return videoFile;
}

您可以停止mediaRecorder之后调用mergeVideos()方法。

You can call mergeVideos() method after stopping mediaRecorder.

希望这code可以帮助你。 :)

Hope this code helps you. :)

有关合并的视频,你必须使用isoparser库。所以,你必须添加以下的中的gradle文件dependendy:

For merging the videos you have to use the isoparser library. So you have to add following dependendy in your gradle file :

compile 'com.googlecode.mp4parser:isoparser:1.0.5.4'

这篇关于点击并按住录制视频像藤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆