扣丁學堂筆記第23天Camera、音訊錄製與Vitamio框架
阿新 • • 發佈:2019-01-29
1.Camera
概述與拍照
錄製視訊
拍照與錄頻程式碼
MainActivity
package com.example.cameratest; import java.io.File; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; public class MainActivity extends Activity { private static final int MEDIA_TYPE_IMAGE = 1; private static final int MEDIA_TYPE_VIDEO = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void imageCaptureClick(View view) { Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFile(MEDIA_TYPE_IMAGE)); startActivityForResult(intent, MEDIA_TYPE_IMAGE); } public void videoCaptureClick(View view) { Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFile(MEDIA_TYPE_VIDEO)); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, MEDIA_TYPE_VIDEO); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ case RESULT_OK: if(MEDIA_TYPE_IMAGE==requestCode){ Uri uri = data.getData(); System.out.println(file); }else if(MEDIA_TYPE_VIDEO==requestCode){ System.out.println(file); System.out.println(data.getData()); } break; } } File file = null; private Uri getOutputMediaFileUri(int type) { file = getOutputMediaFile(type); return Uri.fromFile(file); } private File getOutputMediaFile(int type) { File file = null; String rootPath = null; switch (type) { case MEDIA_TYPE_IMAGE: rootPath = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES).getPath(); file = new File(rootPath + File.separator + ".jpg"); break; case MEDIA_TYPE_VIDEO: rootPath = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_MOVIES).getPath(); file = new File(rootPath + File.separator + ".mp4"); break; } return file; } }
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cameratest.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="拍照" android:onClick="imageCaptureClick"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:text="錄製視訊" android:onClick="videoCaptureClick"/> </RelativeLayout>
AndroidManifest
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2.音訊錄製
MainActivity
package com.example.mediarecordtest; import java.io.File; import java.io.IOException; import android.app.Activity; import android.media.MediaRecorder; import android.media.MediaRecorder.OnErrorListener; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements OnErrorListener{ private MediaRecorder mr; private boolean prepared = false; private Button btn_recorder,btn_stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_recorder = (Button) findViewById(R.id.button1); btn_stop = (Button) findViewById(R.id.button2); mr = new MediaRecorder(); init(); } private void init() { // TODO Auto-generated method stub mr.reset(); mr.setAudioSource(MediaRecorder.AudioSource.MIC);//設定音源 mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//設定音訊格式 mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//設定音訊解碼 String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+File.separator+System.currentTimeMillis()+".mp3"; mr.setOutputFile(path); try { mr.prepare(); prepared = true; }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } btn_stop.setEnabled(false); } public void recordClick(View view){ if(prepared){ mr.start(); prepared = false; btn_stop.setEnabled(true); btn_recorder.setEnabled(false); } } public void stopClick(View view){ mr.stop(); btn_stop.setEnabled(false); btn_recorder.setEnabled(true); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if(mr!=null){ mr.release(); } } @Override public void onError(MediaRecorder mr, int what, int extra) { // TODO Auto-generated method stub mr.reset(); } }
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mediarecordtest.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="錄製音訊"
android:onClick="recordClick" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:text="停止錄製"
android:onClick="stopClick"/>
</RelativeLayout>
AndroidManifest
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
3.Vitamio框架
簡介與匯入依賴
播放音訊
播放視訊
詳見:http://blog.csdn.net/dt235201314/article/details/50598882