mediarecorder學習,android4.0後mediarecorder start failed的原因

來源:互聯網
上載者:User

標籤:

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的原因

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.