編輯:關於 Android 程式設計
本系列第二篇進入更進階的客製化錄影:使用 SurfaceView 作為預覽畫面,搭配 MediaRecorder 控制錄影流程,即可打造完全自訂的錄影介面。
MediaRecorder 的參數設定會因手機型號而異,需依實際裝置調整。setVideoSource()、setAudioSource()、setOutputFormat()、setVideoEncoder()、setAudioEncoder(),之後再設定其餘參數(可查對應 API 的說明)。package c.c;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private Button mStartButton;
private Button mStopButton;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaRecorder mMediaRecorder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去掉標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 設置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 設置橫屏顯示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
init();
}
private void init() {
mStartButton = (Button) findViewById(R.id.start_button);
mStartButton.setOnClickListener(new ButtonClickListenerImpl());
mStopButton = (Button) findViewById(R.id.stop_button);
mStopButton.setOnClickListener(new ButtonClickListenerImpl());
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void initMediaRecorder() {
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.setVideoFrameRate(20);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.setOutputFile(
Environment.getExternalStorageDirectory() + File.separator
+ System.currentTimeMillis() + ".mp4");
}
private class ButtonClickListenerImpl implements OnClickListener {
public void onClick(View v) {
if (v.getId() == R.id.start_button) {
try {
initMediaRecorder();
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (v.getId() == R.id.stop_button) {
if (mMediaRecorder != null) {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
}
}
}
}
public void surfaceCreated(SurfaceHolder holder) { }
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
public void surfaceDestroyed(SurfaceHolder holder) { }
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/start_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始"
android:layout_weight="1" />
<Button
android:id="@+id/stop_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
透過 SurfaceView 提供預覽畫面、MediaRecorder 負責編碼錄製,就能實現完整的自訂錄影流程。只要掌握好參數設定的順序,並依目標裝置微調解析度與編碼器,即可在不同機型上穩定運作。
Android 銀聯支付快速接入筆記
銀聯支付算是蠻常見的一類支付管道。這篇筆記不談理論,直接記錄 Android App 快速接入的過程,方便之後回頭查閱。 一、環境準備 第一步,把銀聯支付的依賴 ja
從源碼切入,透徹理解Android的weight屬性
最近在看一本堪稱古董的書《50 Android Hacks》,而書中開篇的第一個Hack就是「使用weight屬性實現視圖的居中顯示」。 說實話,weight是一個用法簡
Android UI 性能优化的几个实用抓手
UI 卡顿的原因五花八门,但真正有效的手段其实就那么几条。下面这些技巧在实际开发中基本都会用到,做个归纳。 一、压缩主线程的阻塞时间 任何耗时较长的操作(经验上超过 5
Android EditText 即時統計輸入字數
在做輸入框字數統計時,例如顯示剩餘字數或已輸入字數,可以使用 EditText 提供的 addTextChangedListener 來監聽文字變化。實作重點是在 Tex