我想像Snapchat一样录制视频,我试图像这样创建录制按钮
并且当用户长按按钮时录制视频并在用户离开时停止录制,以便如何构建它?请guyz帮助我!!!
答案 0 :(得分:4)
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;
}