• mediarecorder学习,android4.0后mediarecorder start failed的原因


    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 {//ActionBarActivity
    
    	private 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
        {
    		@Override
    		public 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
    {
    
    	@Override
    	public void surfaceCreated(SurfaceHolder holder) {
    		
    		//录像
        	mCamera=Camera.open();
        	try {
    			mCamera.setPreviewDisplay(holder);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
        	mCamera.startPreview();
    		
    	}
    
    	@Override
    	public void surfaceChanged(SurfaceHolder holder, int format, int width,
    			int height) {
    	}
    
    	@Override
    	public void surfaceDestroyed(SurfaceHolder holder) {
    //		mCamera.release();
    //		mCamera=null;
    	}
    	
    }
    }
    

      

  • 相关阅读:
    oracle 安装
    C++中 接口的定义 COM
    linux查看及改变运行级别
    BSD Apache GPL LGPL MIT
    DES MAC PIN HEX
    c语言字符输出格式化
    oracle
    Windows C++ 子目录数量
    GetProcAddress 宏
    How do I install Adobe Flash on Debian Wheezy?
  • 原文地址:https://www.cnblogs.com/bokeofzp/p/4743670.html
Copyright © 2020-2023  润新知