Android MediaRecorder 后台视频录制

题记:记录下这几天一直在苦恼的一件事,心情各种起伏。先是有个变态的客户(嗯,真心觉得)提出原有的录音不够,还要有录像,不过是悄悄的,后台录像,懂的,不能在软件界面上面展示出来;这不是不道德的吗?可是木办法,咱只是做事的,我只能说从技术上肯定是可行,但是道德上,感觉挺混蛋的。于是开始学习看如何做。由于使用API提供的自定义视频,必须要按照那一套写法,感觉中间最关键还是是必须要有surfaceview这个界面预览,说白了,是必须在界面上面展现的。然后各种尝试,闲话不说啦,下面记录下尝试的过程。

一、制作demo。
demo主要实现的功能是activity一启动的时候就自动的启动一后台线程进行录像。当然,录像有时间限制,到时间后自动关闭。同时不显示预览效果。
主要用到了三个文件:RecordDemoActivity 主界面,RecordThread录像线程以及主界面的样式文件main.xml。

1. RecordDemoActivity  

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">package</span><span class="pln" style="color: rgb(0, 0, 0);"> com</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">demo</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>


<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">app</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Activity</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">os</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Bundle</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">view</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">view</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceView</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">widget</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">LinearLayout</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);"> * class name:RecordDemoActivity<BR></span>
<span class="com" style="color: rgb(136, 0, 0);"> * class description:demo<BR></span>
<span class="com" style="color: rgb(136, 0, 0);"> *<BR></span>
<span class="com" style="color: rgb(136, 0, 0);"> * </span>
<span class="com" style="color: rgb(136, 0, 0);"> * @version 1.0 2012/08/02</span>
<span class="com" style="color: rgb(136, 0, 0);"> * @author bcaiw</span>
<span class="com" style="color: rgb(136, 0, 0);"> */</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">RecordDemoActivity</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">extends</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Activity</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">implements</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Callback</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceView</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceview</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="com" style="color: rgb(136, 0, 0);">// 视频预览控件</span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">LinearLayout</span><span class="pln" style="color: rgb(0, 0, 0);"> lay</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//愿揽控件的</span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//和surfaceView相关的</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">     * onCreate方法</span>
<span class="com" style="color: rgb(136, 0, 0);">     */</span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> onCreate</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Bundle</span><span class="pln" style="color: rgb(0, 0, 0);"> savedInstanceState</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="kwd" style="color: rgb(0, 0, 136);">super</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">onCreate</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">savedInstanceState</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">       </span>
<span class="pln" style="color: rgb(0, 0, 0);">        setContentView</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">layout</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">main</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="com" style="color: rgb(136, 0, 0);">//初始化控件</span>
<span class="pln" style="color: rgb(0, 0, 0);">        init</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">     * 初始化控件以及回调</span>
<span class="com" style="color: rgb(136, 0, 0);">     */</span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> init</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">       </span>
<span class="pln" style="color: rgb(0, 0, 0);">        surfaceview </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceView</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">findViewById</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">id</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">surfaceview</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        lay</span><span class="pun" style="color: rgb(102, 102, 0);">=(</span><span class="typ" style="color: rgb(102, 0, 102);">LinearLayout</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">findViewById</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">R</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">id</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">lay</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(255, 0, 0);"><span class="pln" style="color: rgb(0, 0, 0);"> lay</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setVisibility</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">LinearLayout</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">INVISIBLE</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pln" style="color: rgb(0, 0, 0);"> holder </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">surfaceview</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getHolder</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="com" style="color: rgb(136, 0, 0);">// 取得holder </span>
<span class="pln" style="color: rgb(0, 0, 0);">     holder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">addCallback</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">// holder加入回调接口 </span>
<span class="pln" style="color: rgb(0, 0, 0);">     </span><span class="com" style="color: rgb(136, 0, 0);">// 设置setType</span>
<span class="pln" style="color: rgb(0, 0, 0);">     holder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setType</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">SURFACE_TYPE_PUSH_BUFFERS</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">         </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceChanged</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pln" style="color: rgb(0, 0, 0);"> holder</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln" style="color: rgb(0, 0, 0);"> format</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln" style="color: rgb(0, 0, 0);"> width</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">            </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln" style="color: rgb(0, 0, 0);"> height</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="com" style="color: rgb(136, 0, 0);">// 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder </span>
<span class="pln" style="color: rgb(0, 0, 0);">        surfaceHolder </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> holder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceCreated</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pln" style="color: rgb(0, 0, 0);"> holder</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="com" style="color: rgb(136, 0, 0);">// 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder </span>
<span class="pln" style="color: rgb(0, 0, 0);">        surfaceHolder </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> holder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">     </span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">i</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"process"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">Thread</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">currentThread</span><span class="pun" style="color: rgb(102, 102, 0);">().</span><span class="pln" style="color: rgb(0, 0, 0);">getName</span><span class="pun" style="color: rgb(102, 102, 0);">());</span>
<span class="pln" style="color: rgb(0, 0, 0);">     </span><span class="com" style="color: rgb(136, 0, 0);">//录像线程,当然也可以在别的地方启动,但是一定要在onCreate方法执行完成以及surfaceHolder被赋值以后启动</span>
<span class="pln" style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(255, 0, 0);"><span class="typ" style="color: rgb(102, 0, 102);">RecordThread</span><span class="pln" style="color: rgb(0, 0, 0);"> thread</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">RecordThread</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1000</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">surfaceview</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);">surfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></span>
<span class="pln" style="color: rgb(0, 0, 0);">        thread</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">start</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceDestroyed</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pln" style="color: rgb(0, 0, 0);"> holder</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="com" style="color: rgb(136, 0, 0);">// surfaceDestroyed的时候同时对象设置为null </span>
<span class="pln" style="color: rgb(0, 0, 0);">        surfaceview </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">null</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        surfaceHolder </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">null</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="pln" style="color: rgb(0, 0, 0);">        </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span></p>

2. RecordThread

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">package</span><span class="pln" style="color: rgb(0, 0, 0);"> com</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">demo</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">io</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">IOException</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Date</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Timer</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">TimerTask</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>

<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">hardware</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Camera</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">media</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">CamcorderProfile</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">media</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">MediaRecorder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">view</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln" style="color: rgb(0, 0, 0);"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">view</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceView</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>

<span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);"> * 录像线程</span>
<span class="com" style="color: rgb(136, 0, 0);"> * </span>
<span class="com" style="color: rgb(136, 0, 0);"> * @author bcaiw</span>
<span class="com" style="color: rgb(136, 0, 0);"> * </span>
<span class="com" style="color: rgb(136, 0, 0);"> */</span>
<span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">RecordThread</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">extends</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Thread</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>

<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">MediaRecorder</span><span class="pln" style="color: rgb(0, 0, 0);"> mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="com" style="color: rgb(136, 0, 0);">// 录制视频的类</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">long</span><span class="pln" style="color: rgb(0, 0, 0);"> recordTime</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceView</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceview</span><span class="pun" style="color: rgb(102, 102, 0);">;</span><span class="com" style="color: rgb(136, 0, 0);">// 显示视频的控件</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Camera</span><span class="pln" style="color: rgb(0, 0, 0);"> mCamera</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">RecordThread</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">long</span><span class="pln" style="color: rgb(0, 0, 0);"> recordTime</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceView</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceview</span><span class="pun" style="color: rgb(102, 102, 0);">,</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="typ" style="color: rgb(102, 0, 102);">SurfaceHolder</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">recordTime </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> recordTime</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">surfaceview </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceview</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">surfaceHolder </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> surfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> run</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>

<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">   * 开始录像</span>
<span class="com" style="color: rgb(136, 0, 0);">   */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  startRecord</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>

<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">   * 启动定时器,到规定时间recordTime后执行停止录像任务</span>
<span class="com" style="color: rgb(136, 0, 0);">   */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">Timer</span><span class="pln" style="color: rgb(0, 0, 0);"> timer </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Timer</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>

<span class="pln" style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(255, 0, 0);"><span class="pln" style="color: rgb(0, 0, 0);">timer</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">schedule</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">TimerThread</span><span class="pun" style="color: rgb(102, 102, 0);">(),</span><span class="pln" style="color: rgb(0, 0, 0);"> recordTime</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

<span class="pln" style="color: rgb(0, 0, 0);"> </span>

<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">  * 获取摄像头实例对象</span>
<span class="com" style="color: rgb(136, 0, 0);">  * </span>
<span class="com" style="color: rgb(136, 0, 0);">  * @return</span>
<span class="com" style="color: rgb(136, 0, 0);">  */</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Camera</span><span class="pln" style="color: rgb(0, 0, 0);"> getCameraInstance</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">Camera</span><span class="pln" style="color: rgb(0, 0, 0);"> c </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">null</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">   c </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Camera</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">open</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Exception</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="com" style="color: rgb(136, 0, 0);">// 打开摄像头错误</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">i</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"info"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="str" style="color: rgb(0, 136, 0);">"打开摄像头错误"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);"> c</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">  * 开始录像</span>
<span class="com" style="color: rgb(136, 0, 0);">  */</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> startRecord</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  mediarecorder </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">MediaRecorder</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="com" style="color: rgb(136, 0, 0);">// 创建mediarecorder对象</span>
<span class="pln" style="color: rgb(0, 0, 0);">  mCamera </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> getCameraInstance</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">// 解锁camera</span>
<span class="pln" style="color: rgb(0, 0, 0);">  mCamera</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">unlock</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">  mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setCamera</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">mCamera</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>

<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">// 设置录制视频源为Camera(相机)</span>
<span class="pln" style="color: rgb(0, 0, 0);">  mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setAudioSource</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">MediaRecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">AudioSource</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">CAMCORDER</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setVideoSource</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">MediaRecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">VideoSource</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">CAMERA</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>

<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">// 设置录制文件质量,格式,分辨率之类,这个全部包括了</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(255, 0, 0);"><span class="pln" style="color: rgb(0, 0, 0);">mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setProfile</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">CamcorderProfile</span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">get</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">CamcorderProfile</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">QUALITY_LOW</span><span class="pun" style="color: rgb(102, 102, 0);">));</span></span>

<span class="pln" style="color: rgb(0, 0, 0);">  mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setPreviewDisplay</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">surfaceHolder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">getSurface</span><span class="pun" style="color: rgb(102, 102, 0);">());</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">// 设置视频文件输出的路径</span>
<span class="pln" style="color: rgb(0, 0, 0);">  mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">setOutputFile</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"/sdcard/sForm.3gp"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="com" style="color: rgb(136, 0, 0);">// 准备录制</span>
<span class="pln" style="color: rgb(0, 0, 0);">   mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">prepare</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="com" style="color: rgb(136, 0, 0);">// 开始录制</span>
<span class="pln" style="color: rgb(0, 0, 0);">   mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">start</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">IllegalStateException</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="com" style="color: rgb(136, 0, 0);">// TODO Auto-generated catch block</span>
<span class="pln" style="color: rgb(0, 0, 0);">   e</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">printStackTrace</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">IOException</span><span class="pln" style="color: rgb(0, 0, 0);"> e</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="com" style="color: rgb(136, 0, 0);">// TODO Auto-generated catch block</span>
<span class="pln" style="color: rgb(0, 0, 0);">   e</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">printStackTrace</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">  * 停止录制</span>
<span class="com" style="color: rgb(136, 0, 0);">  */</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> stopRecord</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">mediarecorder </span><span class="pun" style="color: rgb(102, 102, 0);">!=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">null</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="com" style="color: rgb(136, 0, 0);">// 停止录制</span>
<span class="pln" style="color: rgb(0, 0, 0);">   mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">stop</span><span class="pun" style="color: rgb(102, 102, 0);">();</span><span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="com" style="color: rgb(136, 0, 0);">// 释放资源</span>
<span class="pln" style="color: rgb(0, 0, 0);">   mediarecorder</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">release</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">   mediarecorder </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">null</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>

<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">mCamera</span><span class="pun" style="color: rgb(102, 102, 0);">!=</span><span class="kwd" style="color: rgb(0, 0, 136);">null</span><span class="pun" style="color: rgb(102, 102, 0);">){</span>
<span class="pln" style="color: rgb(0, 0, 0);">    mCamera</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">release</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">    mCamera </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">null</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln" style="color: rgb(0, 0, 0);">   </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>

<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">  * 定时器</span>
<span class="com" style="color: rgb(136, 0, 0);">  * @author bcaiw</span>
<span class="com" style="color: rgb(136, 0, 0);">  *</span>
<span class="com" style="color: rgb(136, 0, 0);">  */</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">TimerThread</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">extends</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">TimerTask</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>

<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/**</span>
<span class="com" style="color: rgb(136, 0, 0);">   * 停止录像</span>
<span class="com" style="color: rgb(136, 0, 0);">   */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> run</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(255, 0, 0);"><span class="pln" style="color: rgb(0, 0, 0);">stopRecord</span><span class="pun" style="color: rgb(102, 102, 0);">();</span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">cancel</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>
<span class="pun" style="color: rgb(102, 102, 0);">}</span>
</p><div>
</div>

3.main.xml。
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="tag" style="color: rgb(0, 0, 136);"><LinearLayout</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">xmlns:android</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"http://schemas.android.com/apk/res/android"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="atn" style="color: rgb(102, 0, 102);">android:layout_width</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"fill_parent"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="atn" style="color: rgb(102, 0, 102);">android:layout_height</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"fill_parent"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="atn" style="color: rgb(102, 0, 102);">android:orientation</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"horizontal"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="tag" style="color: rgb(0, 0, 136);"><LinearLayout</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="atn" style="color: rgb(102, 0, 102);">android:layout_width</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"fill_parent"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="atn" style="color: rgb(102, 0, 102);">android:layout_height</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"fill_parent"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="atn" style="color: rgb(102, 0, 102);">android:layout_weight</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"1"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">       </span><span style="color: rgb(255, 0, 0);"><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(102, 0, 102);">android:id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"@+id/lay"</span></span><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">        </span><span class="tag" style="color: rgb(0, 0, 136);"><SurfaceView</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">            </span><span class="atn" style="color: rgb(102, 0, 102);">android:id</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"@+id/surfaceview"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">            </span><span class="atn" style="color: rgb(102, 0, 102);">android:layout_width</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"fill_parent"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">            </span><span class="atn" style="color: rgb(102, 0, 102);">android:layout_height</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">"fill_parent"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="tag" style="color: rgb(0, 0, 136);"></LinearLayout></span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">   </span>
<span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="tag" style="color: rgb(0, 0, 136);"></LinearLayout></span><span class="pln" style="color: rgb(0, 0, 0);"> </span></p>

demo简单的实现了一个大体过程。分析下:

a.activity里面提供surfaceview,但是其本身不允许设置隐藏的,可能api他本身有针对这个控件的检查机制,那么试想把他的父控件设置成invisable的,他不会连父控件也检查吧。将ld为lay的linearlayout控件设置成INVISABLE,事实证明录像的时候,预览不可见了,可行。

b.录像时必须要surfaceview对象,而线程是没有界面元素的,因此,实例化线程对象的时候,将surfaceview以及holder对象传递进来。

c.由于是后台的,因此需要有时间限制,时间一到,自动停止。定义一个定时器,Timer子类,重写run方法就可以了。

d.mediarecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));这一句顶n句,包括了设置视频文件的格式,分辨率,码率等等。当然,如果想要自由一点,还是一个一个设。


这样录制隐藏视频的两大条件:【不显示界面】、【到时间自动停止】都已具备。少年们,可以搞起。


但是这还仅仅是个demo,一个demo而已。并不能马上结合到现有的项目中来,结合之前,且来分析分析它所缺的:

a.缺少交互。虽然是个线程单独录制,但是万一录制的过程中需要停下来怎么办?

b.启动和关闭的声音没解决。这个真心不知道怎么弄。

c.没有控制,主线程不知道你有没有在规定的时间内录制完,就是不知道你这个线程的状态,进而就不知道录像设备的到底是不是空着的,如果不是空着的,又是被谁用的。

d.异常处理。各种异常了,怎么办。

e.和其他如录音、拍照冲突了如何解决?经过实验,三者是不同共存的,就是说同时有录音和录像的时候,录像文件是损坏的。同时有录像和拍照的时候,拍照程序一启动,就说设备故障。这中间该如何协调处理?

目前能想到所可能面临的就这些,那么下篇分析引进多线程机制,主从线程直接相互通信来传递消息,就是我了解你在想什么,你也了解我想干什么。还有就是如何解决照相和视频冲突的问题。


后记:

技术这个,是越往下做,越觉得自己不懂的是真多。哈哈哈,今天看见一句话,傻逼的坚持也有牛逼的一天。哈哈哈,唯有不断前进。

已标记关键词 清除标记
相关推荐
特色: (1)循环录像 (2)自动影片分段 (3)影片播放功能 依日期时间搜寻影片 播放/暂停/前进/后退/上一段/下一段/选择影片 等完整播放功能 (4)紧急电话 快速拨打接口 紧急联络人列表管理 (5)显示目前时间 (6)显示目前速度 (需要开启GPS) (7)自定义功能 预留可使用的录像空间 可选择影片分段时间 可选择不同的分辨率及录像质量(android 2.2 or higher) 320x240 (QVGA) 480x320 (HVGA) 640x480 (VGA) 800x480 (WVGA) 1280x720 (WXGA) 屏幕休眠时间 速度单位选择 是否使用GPS功能 (8)行车轨迹记录 (需要开启GPS) (9)提供地图显示目前位置 (需要开启GPS及网络) (10)播放行车记录档时也会同步播放轨迹记录(requires GPS and network) (11)透过Email或Youtube分享您的影片 (12)可对重要的影片进行标记,标记后的影片将置于"\MyCarRecorder\Marked records"集中管理,不会被循环覆盖 (13)背景录像功能,可让您同时开启其他程序进行多任务作业。 (14)实时标记功能,让您将目前录像存至标记目录 请注意!! 切换至背景录像功能进行多任务作业时需耗损大量系统资源,请视您的手机效能斟酌使用。 如果您对iPhone或iPad上的行车录像有兴趣,可以在iTune上的搜寻输入MyCar Recorder,找到此软件的iPhone/iPad版本。 注: (1)行车录像档将置于SD卡的/MyCarRecorder/目录下 (2)有些机器只支持部份的影片分辨率及质量。如果您的机器不支持您所选的影片分辨率而导致摄影镜头运作有问题,请重新启动并选择其他分辨率。
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页