Lập trình Android: ứng dụng quay Video docx

8 472 2
Lập trình Android: ứng dụng quay Video docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Quay Video Ở hầu hết các điện thoại có hệ điều hành Android đều có hỗ trợ Camera. Và trong bài viết này mình sẽ xây dựng 1 chương trình xử dụng Camera của thiết bị để quay video. Các bước làm nên chương trình như sau: 1/ Tạo Project : Project name: RecordVideo Build Target: Android 2.3.3 Application name: RecordVideo Package name: com.dac.RecordVideo Create Activity: MainActivity 2/ Trong file main.xml các bạn thiết kế như sau: <?xml version="1.0" encoding="utf-8"?> <! This file is /res/layout-land/main.xml > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/initBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Initialize Recorder" android:onClick="doClick" android:enabled="false" /> <Button android:id="@+id/beginBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Begin Recording" android:onClick="doClick" android:enabled="false" /> <Button android:id="@+id/stopBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop Recording" android:onClick="doClick" /> <Button android:id="@+id/playRecordingBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN android:text="Play Recording" android:onClick="doClick" /> <Button android:id="@+id/stopPlayingRecordingBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop Playing" android:onClick="doClick" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/recording" android:text=" " android:textColor="#FF0000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <VideoView android:id="@+id/videoView" android:layout_width="250dip" android:layout_height="200dip" /> </LinearLayout> </LinearLayout> 3/ Trong file MainActivity.java: package com.dac.RecordVideo; import java.io.File; import java.io.IOException; import android.app.Activity; import android.hardware.Camera; import android.media.MediaRecorder; import android.media.MediaRecorder.OnErrorListener; import android.media.MediaRecorder.OnInfoListener; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.SurfaceHolder; import android.view.View; import android.widget.Button; import android.widget.MediaController; import android.widget.TextView; import android.widget.Toast; import android.widget.VideoView; public class MainActivity extends Activity implements SurfaceHolder.Callback, OnInfoListener, OnErrorListener { private static final String TAG = "RecordVideo"; private MediaRecorder mRecorder = null; private String mOutputFileName; private VideoView mVideoView = null; private SurfaceHolder mHolder = null; Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN private Button mInitBtn = null; private Button mStartBtn = null; private Button mStopBtn = null; private Button mPlayBtn = null; private Button mStopPlayBtn = null; private Camera mCamera = null; private TextView mRecordingMsg = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "in onCreate"); setContentView(R.layout.main); mInitBtn = (Button) findViewById(R.id.initBtn); mStartBtn = (Button) findViewById(R.id.beginBtn); mStopBtn = (Button) findViewById(R.id.stopBtn); mPlayBtn = (Button) findViewById(R.id.playRecordingBtn); mStopPlayBtn = (Button) findViewById(R.id.stopPlayingRecordingBtn); mRecordingMsg = (TextView) findViewById(R.id.recording); mVideoView = (VideoView)this.findViewById(R.id.videoView); } private boolean initCamera() { try { mCamera = Camera.open(); Camera.Parameters camParams = mCamera.getParameters(); mCamera.lock(); mHolder = mVideoView.getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } catch(RuntimeException re) { Log.v(TAG, "Could not initialize the Camera"); re.printStackTrace(); return false; } return true; } private void releaseRecorder() { if(mRecorder != null) { mRecorder.release(); mRecorder = null; } } private void releaseCamera() { if(mCamera != null) { try { Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN mCamera.reconnect(); } catch (IOException e) { e.printStackTrace(); } mCamera.release(); mCamera = null; } } @Override protected void onResume() { Log.v(TAG, "in onResume"); super.onResume(); mInitBtn.setEnabled(false); mStartBtn.setEnabled(false); mStopBtn.setEnabled(false); mPlayBtn.setEnabled(false); mStopPlayBtn.setEnabled(false); if(!initCamera()) finish(); } @Override protected void onPause() { Log.v(TAG, "in onPause"); super.onPause(); releaseRecorder(); releaseCamera(); } @Override protected void onDestroy() { Log.v(TAG, "in onDestroy"); super.onDestroy(); } public void doClick(View view) { switch(view.getId()) { case R.id.initBtn: initRecorder(); break; case R.id.beginBtn: beginRecording(); break; case R.id.stopBtn: stopRecording(); break; case R.id.playRecordingBtn: playRecording(); break; case R.id.stopPlayingRecordingBtn: stopPlayingRecording(); Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN break; } } @Override public void surfaceCreated(SurfaceHolder holder) { Log.v(TAG, "in surfaceCreated"); try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (IOException e) { Log.v(TAG, "Could not start the preview"); e.printStackTrace(); } mInitBtn.setEnabled(true); } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.v(TAG, "in surfaceDestroyed"); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.v(TAG, "surfaceChanged: Width x Height = " + width + "x" + height); } private void initRecorder() { if(mRecorder != null) return; mOutputFileName = Environment.getExternalStorageDirectory() + "/videooutput.mp4"; File outFile = new File(mOutputFileName); if(outFile.exists()) { outFile.delete(); } try { mCamera.stopPreview(); mCamera.unlock(); mRecorder = new MediaRecorder(); mRecorder.setCamera(mCamera); mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mRecorder.setVideoSize(176, 144); mRecorder.setVideoFrameRate(15); mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); Lập trình Android – http://laptrinhdidong.vn Page 5 Trung tâm Tin học – ĐH KHTN mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setMaxDuration(7000); // limit to 7 seconds mRecorder.setPreviewDisplay(mHolder.getSurface()); mRecorder.setOutputFile(mOutputFileName); mRecorder.prepare(); Log.v(TAG, "MediaRecorder initialized"); mInitBtn.setEnabled(false); mStartBtn.setEnabled(true); } catch(Exception e) { Log.v(TAG, "MediaRecorder failed to initialize"); e.printStackTrace(); } } private void beginRecording() { mRecorder.setOnInfoListener(this); mRecorder.setOnErrorListener(this); mRecorder.start(); mRecordingMsg.setText("RECORDING"); mStartBtn.setEnabled(false); mStopBtn.setEnabled(true); } private void stopRecording() { if (mRecorder != null) { mRecorder.setOnErrorListener(null); mRecorder.setOnInfoListener(null); try { mRecorder.stop(); } catch(IllegalStateException e) { Log.e(TAG, "Got IllegalStateException in stopRecording"); } releaseRecorder(); mRecordingMsg.setText(""); releaseCamera(); mStartBtn.setEnabled(false); mStopBtn.setEnabled(false); mPlayBtn.setEnabled(true); } } private void playRecording() { MediaController mc = new MediaController(this ); mVideoView.setMediaController(mc); mVideoView.setVideoPath(mOutputFileName); mVideoView.start(); mStopPlayBtn.setEnabled(true); } Lập trình Android – http://laptrinhdidong.vn Page 6 Trung tâm Tin học – ĐH KHTN private void stopPlayingRecording() { mVideoView.stopPlayback(); } @Override public void onInfo(MediaRecorder mr, int what, int extra) { Log.i(TAG, "got a recording event"); if(what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) { Log.i(TAG, " max duration reached"); stopRecording(); Toast.makeText(this, "Recording limit has been reached. Stopping the recording", Toast.LENGTH_SHORT).show(); } } @Override public void onError(MediaRecorder mr, int what, int extra) { Log.e(TAG, "got a recording error"); stopRecording(); Toast.makeText(this, "Recording error has occurred. Stopping the recording", Toast.LENGTH_SHORT).show(); } } Và trong phần AndroidManifest.xml các bạn thêm các Permission vào ứng dụng: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dac.Recordvideo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> Lập trình Android – http://laptrinhdidong.vn Page 7 Trung tâm Tin học – ĐH KHTN <uses-permission android:name="android.permission.CAMERA"/> </manifest> Cuối cùng các bạn debug ứng dụng (lưu ý ứng dụng chỉ chạy video trên máy thật) và các bạn bấm Ctrl+F11 để chuyển qua màn hình ngang : Mọi ý kiến đóng góp các bạn vui lòng gữi bài viết về forum : http://forum.laptrinhdidong.vn . Rất mong nhận được sự phản hồi của các bạn. Lập trình Android – http://laptrinhdidong.vn Page 8 . – ĐH KHTN Quay Video Ở hầu hết các điện thoại có hệ điều hành Android đều có hỗ trợ Camera. Và trong bài viết này mình sẽ xây dựng 1 chương trình xử dụng Camera của thiết bị để quay video. Các. android:textColor="#FF0000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <VideoView android:id="@+id/videoView" android:layout_width="250dip". new MediaController(this ); mVideoView.setMediaController(mc); mVideoView.setVideoPath(mOutputFileName); mVideoView.start(); mStopPlayBtn.setEnabled(true); } Lập trình Android – http://laptrinhdidong.vn

Ngày đăng: 08/08/2014, 15:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan