像SnapChat这样的录制按钮 [英] Recording button like SnapChat

查看:71
本文介绍了像SnapChat这样的录制按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想录制像snapchat这样的视频,我正在尝试创建像这样的录制"按钮

I want to record video like snapchat, I'm trying to create Record button like this

,并在用户长按按钮时录制视频,并在用户离开时停止录制,所以我该如何构建它?请盖茨帮助我!!!

and also record video when user long press on button and stop recording when user leave it so how can i built it? Please guyz help me!!!

推荐答案

private static final int MIN_CLICK_DURATION = 600;
private long startClickTime;
private boolean longClickActive;
private boolean recording, pause = false;
private long elapsed;
private long remaningSecs = 0;
private long elapsedSecs = 0;
private Timer timer;

try {

your_button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                    final long INTERVAL = 1000;
                    final long TIMEOUT = 10000;
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:

                            Log.i("ACTION_DOWN", "ACTION_DOWN::" + pause + " " + recording);
                            if (longClickActive == false) {
                                longClickActive = true;
                                startClickTime = Calendar.getInstance().getTimeInMillis();
                            }
                            break;
                        case MotionEvent.ACTION_MOVE:
                            if (longClickActive == true) {
                                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                                if (clickDuration >= MIN_CLICK_DURATION) {
                                    longClickActive = false;
                                    if (pause && !recording) {
                                        pause = false;
                                        if (!prepareMediaRecorder()) {

                                            Log.e("Fail to prapare","");
                                            finish();
                                        }
                                        // work on UiThread for better performance
                                        runOnUiThread(new Runnable() {
                                            public void run() {
                                                try {
                                                    mediaRecorder.start();
                                                } catch (final Exception ex) {
                                                    // Log.i("---","Exception in thread");
                                                }
                                            }
                                        });

                                        recording = true;
                                        TimerTask task = new TimerTask() {
                                            @Override
                                            public void run() {
                                                remaningSecs -= INTERVAL;

                                                if (remaningSecs == 0) {
                                                    this.cancel();
                                                    try {
                                                        runOnUiThread(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                recording = false;
                                                                // TODO Auto-generated method stub
                                                                releaseMediaRecorder(); // release the MediaRecorder object
                                                                prgrsBar.setProgress(0);

                                                                Log.e("Video captured!","");
                                                            }
                                                        });
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                    return;
                                                }
                                                elapsedSecs = remaningSecs;
                                                prgrsBar.setProgress((int) (elapsedSecs / 1000));
                                                Log.i(TAG, "Milli::" + (remaningSecs / 1000));

                                            }
                                        };
                                        timer = new Timer();
                                        timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);

                                    } else {
                                        if (!prepareMediaRecorder()) {

                                             Log.e("Fail in prepare MediaRecorder","");

                                        }
                                        // work on UiThread for better performance
                                        runOnUiThread(new Runnable() {
                                            public void run() {
                                                try {
                                                    mediaRecorder.start();

                                                } catch (final Exception ex) {
                                                    // Log.i("---","Exception in thread");
                                                }
                                            }
                                        });
                                        recording = true;
                                        TimerTask task = new TimerTask() {
                                            @Override
                                            public void run() {
                                                elapsed += INTERVAL;
                                                if (elapsed == 11000) {
                                                    this.cancel();
                                                    try {
                                                        runOnUiThread(new Runnable() {
                                                            @Override
                                                            public void run() {
                                                                recording = false;
                                                                // TODO Auto-generated method stub
                                                                // mediaRecorder.stop(); // stop the recording
                                                                releaseMediaRecorder(); // release the MediaRecorder object
                                                                prgrsBar.setProgress(0);

                                                                Log.e("Video captured!","");
                                                            }
                                                        });


                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                    return;
                                                }
                                                elapsedSecs = 10000 - elapsed;
                                                prgrsBar.setProgress((int) (elapsedSecs / 1000));
                                                Log.i(TAG, "Milli::" + (elapsedSecs / 1000));
                                                remaningSecs = elapsedSecs;
                                                remaningSecs = Math.round(remaningSecs);
                                                elapsedSecs = Math.round(elapsedSecs);
                                            }
                                        };
                                        timer = new Timer();
                                        timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);
                                    }
                                }
                            }
                            break;
                        case MotionEvent.ACTION_UP:
                            Log.i("ACTION_UP", "ACTION_UP::" + recording);
                            longClickActive = false;
                            if (recording) {
                                // stop recording and release camera
                                prgrsBar.setProgress((int) (elapsedSecs / 1000));
                                timer.cancel();
                                pause = true;
                                recording = false;
                                remaningSecs = remaningSecs + 1000;
                                releaseMediaRecorder(); // release the MediaRecorder object
                            }
                            break;
                    }

                return true;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }



//  If applicable to you otherwise use your methods to release and prepare 
private void releaseMediaRecorder() {
    try {
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.reset(); // clear recorder configuration
            mediaRecorder.release(); // release the recorder object
            mediaRecorder = null;
            mCamera.lock(); // lock camera for later use
        }
    } catch (RuntimeException stopException) {
        //handle cleanup here
    }
}

private boolean prepareMediaRecorder() {
    mCamera.unlock();
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setCamera(mCamera);

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    //        mediaRecorder.setProfile(CamcorderProfile
    //                .get(CamcorderProfile.QUALITY_480P));
    mediaRecorder.setProfile(CamcorderProfile.get(1, CamcorderProfile.QUALITY_HIGH));

    mediaRecorder.setOutputFile(strRecordedVideoPath);
    mediaRecorder.setMaxDuration(10000); // Set max duration 10 sec.
    mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M
    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    return true;

}

这篇关于像SnapChat这样的录制按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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