標籤:
1、因為Api的相容問題,,mediarecorder的啟動方式也不同了,因為我看的視頻是api 8的因此無法啟動,在網上查了了好多資料都沒說清楚,最終還是去官網查看才得以實現:
官網實現的方式:
我實現的demo,希望給跟我一樣的初學者一點協助,少走彎路:
需要許可權:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
main_xml檔案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context="com.example.mycamera.MainActivity$PlaceholderFragment" > <SurfaceView android:layout_weight="100" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/camera" /> <RelativeLayout android:layout_weight="0" android:layout_width="fill_parent" android:layout_height="wrap_content" ><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始錄製" android:layout_alignParentRight="true" android:id="@+id/pz" android:onClick="luxiang" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" android:id="@+id/dj" android:layout_toLeftOf="@+id/pz" android:onClick="tingzhi" /></RelativeLayout></LinearLayout>
public class MainActivity extends ActionBarActivity {//ActionBarActivityprivate SurfaceView sv;private SurfaceHolder holder;private Camera mCamera;private MediaRecorder recorder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉資訊列 setContentView(R.layout.activity_main); sv=(SurfaceView) findViewById(R.id.camera); holder=sv.getHolder(); holder.addCallback(new MyCallback()); } public void tingzhi(View v) { recorder.release(); recorder=null; } public void luxiang(View v) throws IOException { recorder=new MediaRecorder(); recorder.reset(); mCamera.unlock(); recorder.setCamera(mCamera); recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);//設定音源攝像機 recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));//指定錄影的品質 recorder.setOutputFile("data/data/musics/hahagaga.3gp"); // 第5步:指定預覽輸出 recorder.setPreviewDisplay(holder.getSurface()); try { recorder.prepare(); } catch (Exception e) { recorder.release(); } recorder.start(); // recorder=new MediaRecorder();// recorder.reset();// recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);// recorder.setAudioSource(MediaRecorder.AudioSource.MIC);// recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//// video-size-values=640x480,576x432,480x320,432x240,384x288,352x288,320x240,240x160,176x144////preview-size-values=640x480,576x432,480x320,432x240,384x288,352x288,320x240,240x160,176x144 // recorder.setVideoSize(576, 432);// recorder.setVideoFrameRate(5);// recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);// recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// recorder.setOutputFile("data/data/musics/hahagaga.3gp");// recorder.setPreviewDisplay(holder.getSurface());// try{// recorder.prepare();// }catch(Exception e)// {// System.out.println("準備失敗");// e.printStackTrace();// }// try{// recorder.start();// }catch(Exception e)// {// System.out.println("開始失敗");// e.printStackTrace();// } } class MyPictureCallback implements PictureCallback {@Overridepublic void onPictureTaken(byte[] data, Camera camera) {//dada就是拍照後壓縮的資料File image=new File(Environment.getDataDirectory(),"zp.jpg");FileOutputStream out=null;try { out=new FileOutputStream(image);out.write(data);out.flush();out.close();} catch (Exception e) {e.printStackTrace();Toast.makeText(MainActivity.this, "儲存照片失敗!", Toast.LENGTH_LONG).show();}finally{//mCamera.startPreview();}} } class MyCallback implements Callback{@Overridepublic void surfaceCreated(SurfaceHolder holder) {//錄影 mCamera=Camera.open(); try {mCamera.setPreviewDisplay(holder);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} mCamera.startPreview();}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {//mCamera.release();//mCamera=null;}}}
mediarecorder學習,android4.0後mediarecorder start failed的原因