編輯:關於 Android 程式設計
本系列第四篇示範最省事的做法:直接呼叫系統相機錄影,再用 VideoView 播放剛拍好的影片。整個流程透過 startActivityForResult 取得影片的 URI 即可。
package c.c;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;
import android.app.Activity;
import android.content.Intent;
/**
* Demo 描述:利用系統相機拍攝視頻,並用 VideoView 播放。
*/
public class MainActivity extends Activity {
private final static int VIDEO_CAPTURE = 9527;
private Uri mVideoUri;
private Button mCaptureButton;
private Button mPlayButton;
private VideoView mVideoView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
mCaptureButton = (Button) findViewById(R.id.capture_Button);
mCaptureButton.setOnClickListener(new ButtonClickListenerImpl());
mPlayButton = (Button) findViewById(R.id.play_Button);
mPlayButton.setEnabled(false);
mPlayButton.setOnClickListener(new ButtonClickListenerImpl());
mVideoView = (VideoView) findViewById(R.id.videoView);
}
private class ButtonClickListenerImpl implements OnClickListener {
public void onClick(View v) {
if (v.getId() == R.id.capture_Button) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, VIDEO_CAPTURE);
} else {
mVideoView.setVideoURI(mVideoUri);
mVideoView.start();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURE) {
mVideoUri = data.getData();
mPlayButton.setEnabled(true);
}
}
}
<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">
<Button
android:id="@+id/capture_Button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="Video Capture" />
<Button
android:id="@+id/play_Button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="Video Play" />
<VideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
這種做法的優點是簡單快速,錄影品質與參數全交由系統相機處理;缺點則是可控性較低,無法自訂錄影參數與畫面外觀。若需要更細緻的控制,可參考本系列前幾篇使用 MediaRecorder 與 SurfaceView 的方案。
在 Android Studio 中接入 OpenCV(macOS / Linux 通用流程)
對於工程師來說,配環境這件事永遠比寫代碼更耗神。這篇記錄我在 macOS 上把 OpenCV 3.1 接進 Android Studio 的完整過程,步驟在 Linux
Android Service 結合 GPS 與基地台定位的實作
需求說明 本實例希望 Service 每隔 1 秒執行一次定位,優先使用 GPS,若 GPS 無法取得位置,則改用基地台定位,最後將經緯度即時顯示在畫面上。 所需權限 &
com.android.phone已停止運作?從撥號盤到刷機基帶排查
com.android.phone已停止運作可先清除撥號盤資料,注意通話記錄會被刪除。 若無效,可能是刷機基帶錯誤,或修改DPI導致來電彈窗相容性問題。最後才考慮恢復原廠
Android JSON 資料解析入門教學
JSON 是一種輕量級的資料交換格式,因為格式簡單、易於閱讀,所以在 Android 開發中經常被用來傳輸資料。本文說明 JSON 的基本結構,並示範如何在伺服器端產生