編輯:關於 Android 程式設計
MediaRecorder 不只能錄音,也能錄影。整體流程與錄音幾乎一致,差別在於影片必須同時采集聲音與影像兩路資料。
因此除了呼叫 setAudioSource() 之外,還要多一行 setVideoSource() 指定影像來源(通常是 MediaRecorder.VideoSource.CAMERA)。此外,在設定輸出格式之後,還要補上兩個步驟:
一、設定影片編碼參數——setVideoEncoder()、setVideoEncodingBitRate()、setVideoFrameRate()、setVideoSize()。同樣地,品質與檔案大小呈正比。
二、指定預覽畫面——呼叫 setPreviewDisplay(surface),告訴 MediaRecorder 要用哪個 SurfaceView 顯示即時影像。
public class RecordVideo extends Activity implements OnClickListener {
private ImageButton record, stop;
private File videoFile;
private MediaRecorder mRecorder;
private SurfaceView sView;
private boolean isRecording = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全螢幕錄影的三個前置設定,順序有講究
requestWindowFeature(Window.FEATURE_NO_TITLE); // 必須在 setContentView 之前
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
record = (ImageButton) findViewById(R.id.record);
stop = (ImageButton) findViewById(R.id.stop);
stop.setEnabled(false);
record.setOnClickListener(this);
stop.setOnClickListener(this);
sView = (SurfaceView) findViewById(R.id.sView);
sView.getHolder().setFixedSize(1280, 720);
sView.getHolder().setKeepScreenOn(true); // 錄影中别让螢幕自動關閉
}
@Override
public void onClick(View source) {
switch (source.getId()) {
case R.id.record:
if (!Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(RecordVideo.this, "找不到儲存卡!", Toast.LENGTH_SHORT).show();
return;
}
try {
videoFile = new File(Environment.getExternalStorageDirectory()
.getCanonicalFile() + "/testvideo.3gp");
mRecorder = new MediaRecorder();
mRecorder.reset();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 順序關鍵:輸出格式要在兩種編碼設定之前
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mRecorder.setVideoSize(1280, 720);
mRecorder.setVideoFrameRate(20);
mRecorder.setOutputFile(videoFile.getAbsolutePath());
mRecorder.setPreviewDisplay(sView.getHolder().getSurface());
mRecorder.prepare();
mRecorder.start();
record.setEnabled(false);
stop.setEnabled(true);
isRecording = true;
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.stop:
if (isRecording) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
record.setEnabled(true);
stop.setEnabled(false);
isRecording = false;
}
break;
}
}
}
SurfaceView 舖滿整個畫面當預覽區,兩個按鈕疊在下方:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 影片預覽 -->
<SurfaceView
android:id="@+id/sView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageButton
android:id="@+id/record"
android:layout_width="66dp"
android:layout_height="66dp"
android:scaleType="fitCenter"
android:src="@drawable/recored" />
<ImageButton
android:id="@+id/stop"
android:layout_width="66dp"
android:layout_height="66dp"
android:scaleType="fitCenter"
android:src="@drawable/stop" />
</LinearLayout>
</RelativeLayout>
<!-- 錄音 --> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <!-- 使用相機 --> <uses-permission android:name="android.permission.CAMERA" /> <!-- 寫入外部儲存 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
執行預覽:

requestWindowFeature 必須在 setContentView 之前呼叫,否則會拋出 AndroidRuntimeException。這行的位置在範例程式中刻意標了註解,因為它是最常見的崩潰原因之一。
相機權限在 Android 6.0 以上屬於危險權限,除了 Manifest 宣告,還要在執行期動態申請,並且 CAMERA、RECORD_AUDIO 兩者都要拿到,少一個 prepare() 就會失敗。
錄影時螢幕不會自動關閉,靠的是 setKeepScreenOn(true)。錄完記得處理,不然會一直亮著耗電。
啟動與停止錄製時要互斥切換按鈕狀態,避免使用者連點造成狀態錯亂。
若要錄前置鏡頭,需在 prepare 之前透過 setCamera() 指定已開啟的 Camera 物件,並自行處理開啟與釋放。
NumberPickerView:一款 UI 體驗優於原生 NumberPicker 的自訂控制項
NumberPickerView一款在 Android 平台上具備更靈活屬性的 NumberPicker 替代品。專案地址https://github.com/Carbs
詳解Android Preference首選項的編寫與組織
翻 Android 書籍時常看到「首選項」這個名詞,但多數書直接教你怎麼用,很少說明它從哪來、憑什麼存在。這一篇就先把概念講清楚,再動手做。 首選項到底是什麼 「首選項」
Android 實作:用百度 APIstore 與 JSON 抓取新聞頻道與內容
本文示範一個完整的流程:呼叫百度 APIstore 的新聞介面取得頻道清單,用 Spinner 呈現;使用者挑選頻道後,再請求該頻道的新聞列表並顯示出來。整份程式拆成三
免費送書好康!帶你一起「愛上 Android」
感謝各位長期以來對本站的支持!這次小編又準備了一波送書福利,只要你參與就有機會免費把好书帶回家,快來一起共襄盛舉。 《愛上 Android》 內容提要 本書深入淺出,