java制造仿微信錄制藐視頻控件。本站提示廣大學習愛好者:(java制造仿微信錄制藐視頻控件)文章只能為提供參考,不一定能成為您想要的結果。以下是java制造仿微信錄制藐視頻控件正文
-- 獲得以後的格林尼治時光
print(os.time())
-- 獲得以後時光的字符串表現,形如:11/28/08 10:28:37
print(os.date())
-- 獲得以後日期的字符串表現,形如:11/28/08
print(os.date("%x", os.time()))
-- 獲得以後時光的字符串表現,形如:10:28:37
print(os.date("%X", os.time()))
-- 獲得以後時光的字符串表現,形如:10/10/13 10:28:37
print(os.date("%c", os.time()))
-- 獲得以後時光的字符串表現,形如:2013-10-10 10:28:37
print(os.date("%Y-%m-%d %H:%M:%S", os.time()))
--函數os.clock前往履行該法式CPU花去的時鐘秒數
local x1 = os.clock()
local s = 0
for i = 1, 10000000 do
s = s + i
end
local x2 = os.clock()
print(string.format("elapsed time: %.2f\n", x2 - x1))
local T2009_StartTime = { year=2013, month=2, day=9, hour=0, min=0, sec=0 }
local T2009_EndTime = { year=2013, month=2, day=17, hour=23, min=59, sec=59 }
T2009_AvtivityTime = { startTime = os.time(T2009_StartTime), endTime = os.time(T2009_EndTime) }
print('加載禮包運動勝利,運動時光:' .. os.date('%c', T2009_AvtivityTime.startTime) ..
'~' .. os.date('%c', T2009_AvtivityTime.endTime))
temp = os.date("*t", os.time())
print(temp)
--[[則會發生表
{year = 1998, month = 9, day = 16, yday = 259, wday = 4,
hour = 23, min = 48, sec = 10, isdst = false}
--]]
**
* 視頻播放控件
*
* @author liuyinjun
*
* @date 2015-2-5
*/
public class MovieRecorderView extends LinearLayout implements OnErrorListener {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private ProgressBar mProgressBar;
private MediaRecorder mMediaRecorder;
private Camera mCamera;
private Timer mTimer;// 計時器
private OnRecordFinishListener mOnRecordFinishListener;// 錄制完成回調接口
private int mWidth;// 視頻分辯率寬度
private int mHeight;// 視頻分辯率高度
private boolean isOpenCamera;// 能否一開端就翻開攝像頭
private int mRecordMaxTime;// 一次拍攝最長時光
private int mTimeCount;// 時光計數
private File mVecordFile = null;// 文件
public MovieRecorderView(Context context) {
this(context, null);
}
public MovieRecorderView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MovieRecorderView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MovieRecorderView, defStyle, 0);
mWidth = a.getInteger(R.styleable.MovieRecorderView_width, 320);// 默許320
mHeight = a.getInteger(R.styleable.MovieRecorderView_height, 240);// 默許240
isOpenCamera = a.getBoolean(R.styleable.MovieRecorderView_is_open_camera, true);// 默許翻開
mRecordMaxTime = a.getInteger(R.styleable.MovieRecorderView_record_max_time, 10);// 默許為10
LayoutInflater.from(context).inflate(R.layout.movie_recorder_view, this);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mProgressBar.setMax(mRecordMaxTime);// 設置進度條最年夜量
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(new CustomCallBack());
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
a.recycle();
}
/**
*
* @author liuyinjun
*
* @date 2015-2-5
*/
private class CustomCallBack implements Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!isOpenCamera)
return;
try {
initCamera();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (!isOpenCamera)
return;
freeCameraResource();
}
}
/**
* 初始化攝像頭
*
* @author liuyinjun
* @date 2015-2-5
* @throws IOException
*/
private void initCamera() throws IOException {
if (mCamera != null) {
freeCameraResource();
}
try {
mCamera = Camera.open();
} catch (Exception e) {
e.printStackTrace();
freeCameraResource();
}
if (mCamera == null)
return;
setCameraParams();
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
mCamera.unlock();
}
/**
* 設置攝像頭為豎屏
*
* @author liuyinjun
* @date 2015-2-5
*/
private void setCameraParams() {
if (mCamera != null) {
Parameters params = mCamera.getParameters();
params.set("orientation", "portrait");
mCamera.setParameters(params);
}
}
/**
* 釋放攝像頭資本
*
* @author liuyinjun
* @date 2015-2-5
*/
private void freeCameraResource() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.lock();
mCamera.release();
mCamera = null;
}
}
private void createRecordDir() {
File sampleDir = new File(Environment.getExternalStorageDirectory() + File.separator + "im/video/");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
File vecordDir = sampleDir;
// 創立文件
try {
mVecordFile = File.createTempFile("recording", ".mp4", vecordDir);//mp4格局
LogUtils.i(mVecordFile.getAbsolutePath());
} catch (IOException e) {
}
}
/**
* 初始化
*
* @author liuyinjun
* @date 2015-2-5
* @throws IOException
*/
private void initRecord() throws IOException {
mMediaRecorder = new MediaRecorder();
mMediaRecorder.reset();
if (mCamera != null)
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setOnErrorListener(this);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.setVideoSource(VideoSource.CAMERA);// 視頻源
mMediaRecorder.setAudioSource(AudioSource.MIC);// 音頻源
mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);// 視頻輸入格局
mMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);// 音頻格局
mMediaRecorder.setVideoSize(mWidth, mHeight);// 設置分辯率:
// mMediaRecorder.setVideoFrameRate(16);// 這個我把它去失落了,感到沒甚麼用
mMediaRecorder.setVideoEncodingBitRate(1 * 1024 * 512);// 設置幀頻率,然後就清楚了
mMediaRecorder.setOrientationHint(90);// 輸入扭轉90度,堅持豎屏錄制
mMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP);// 視頻錄制格局
// mediaRecorder.setMaxDuration(Constant.MAXVEDIOTIME * 1000);
mMediaRecorder.setOutputFile(mVecordFile.getAbsolutePath());
mMediaRecorder.prepare();
try {
mMediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 開端錄制視頻
*
* @author liuyinjun
* @date 2015-2-5
* @param fileName
* 視頻貯存地位
* @param onRecordFinishListener
* 到達指准時間以後回調接口
*/
public void record(final OnRecordFinishListener onRecordFinishListener) {
this.mOnRecordFinishListener = onRecordFinishListener;
createRecordDir();
try {
if (!isOpenCamera)// 假如未翻開攝像頭,則翻開
initCamera();
initRecord();
mTimeCount = 0;// 時光計數重視新賦值
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
mTimeCount++;
mProgressBar.setProgress(mTimeCount);// 設置進度條
if (mTimeCount == mRecordMaxTime) {// 到達指准時間,停滯拍攝
stop();
if (mOnRecordFinishListener != null)
mOnRecordFinishListener.onRecordFinish();
}
}
}, 0, 1000);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 停滯拍攝
*
* @author liuyinjun
* @date 2015-2-5
*/
public void stop() {
stopRecord();
releaseRecord();
freeCameraResource();
}
/**
* 停滯錄制
*
* @author liuyinjun
* @date 2015-2-5
*/
public void stopRecord() {
mProgressBar.setProgress(0);
if (mTimer != null)
mTimer.cancel();
if (mMediaRecorder != null) {
// 設置後不會崩
mMediaRecorder.setOnErrorListener(null);
mMediaRecorder.setPreviewDisplay(null);
try {
mMediaRecorder.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 釋放資本
*
* @author liuyinjun
* @date 2015-2-5
*/
private void releaseRecord() {
if (mMediaRecorder != null) {
mMediaRecorder.setOnErrorListener(null);
try {
mMediaRecorder.release();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
mMediaRecorder = null;
}
public int getTimeCount() {
return mTimeCount;
}
/**
* @return the mVecordFile
*/
public File getmVecordFile() {
return mVecordFile;
}
/**
* 錄制完成回調接口
*
* @author liuyinjun
*
* @date 2015-2-5
*/
public interface OnRecordFinishListener {
public void onRecordFinish();
}
@Override
public void onError(MediaRecorder mr, int what, int extra) {
try {
if (mr != null)
mr.reset();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
movie_recorder_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:orientation="vertical">
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="2dp"
/>
</LinearLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:orientation="vertical">
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="2dp"
/>
</LinearLayout>
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。