編輯:關於Android編程
1 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/videoView"
android:layout_width="300dip"
android:layout_height="200dip"
android:visibility="visible" >
</SurfaceView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="錄制" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/start"
android:text="停止" />
</RelativeLayout>
</LinearLayout>
2,java代碼
package prox.AndroidRecordVideoTest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AndroidRecordVideoTest extends Activity {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Button buttonStart;
private Button buttonStop;
private File dir;
private MediaRecorder recorder;
private String path = "/sdcard/RTCameraSendRecv/";
SimpleDateFormat format1;
String fileName = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
format1 = new SimpleDateFormat("yyyyMMddHHmmss");
mSurfaceView = (SurfaceView) findViewById(R.id.videoView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
buttonStart = (Button) findViewById(R.id.start);
buttonStop = (Button) findViewById(R.id.stop);
dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
recorder = new MediaRecorder();
buttonStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startrecord();
}
});
buttonStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
});
}
public void startrecord() {
try {
fileName = format1.format(new Date());
recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoSize(352, 288);
recorder.setVideoFrameRate(25);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setMaxDuration(60000); // 60s
recorder.setOutputFile(path + fileName+".3gp");
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3,在AndroidManifest.xml 加上權限
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
就這樣 一個攝像頭錄像程序就完工了
動態加載資源原理通常我們調用getResources()方法獲取資源文件public Resources getResources() { return mReso
作為一個成熟的應用, 必須要有廣告. 那麼, 如何優雅地開發廣告呢? 需要注意一些細節.本文提供一個簡單的示例, 代碼僅供參考.需求:1. 下載廣告在歡迎頁面中, 啟動一
之所以把PopupWindow與懸浮窗這兩個放到一起講,是因為這兩個的實現原理基本是一致的,只是有點不同而已。 原理: 使用系統服務(WindowManag
這是一個基於Service組件的音樂播放器,程序的音樂將會由後台的Service組件負責播放,當後台的播放狀態改變時,程序將會通過發送廣播通知前台Activity更新界面