Android源码中Record应用相对来说在模块中是比较简单的。文章之前,我把自己如何把源码导入Eclipse的过程写了一篇文档。
文档地址:http://blog.youkuaiyun.com/easy_gemini/article/details/8210934
在Eclipse中跑出来的Record界面:
这里需要强调的是,在模拟器中是不能录音的,手机上是可以的。这是因为
// Handle RuntimeException ifthe recording couldn't start
try {
mRecorder.start(); //为何模拟器录音不了,因为这个函数在运行时候抛出异常,执行了setError(INTERNAL_ERROR);
} catch (RuntimeException exception) {
AudioManager audioMngr =(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
boolean isInCall = audioMngr.getMode()== AudioManager.MODE_IN_CALL;
if (isInCall) {
setError(IN_CALL_RECORD_ERROR);
} else {
setError(INTERNAL_ERROR);
}
根据调试之后确定在模拟器中的mRecorder.start();抛出了异常,走的是setError(INTERNAL_ERROR);
会提示。
进入start()方法后可以看到里面是库文件提供的方法
/**
* Begins capturing and encoding data tothe file specified with
* setOutputFile(). Call this afterprepare().
*
* <p>Since API level 13, if applications set acamera via
* {@link #setCamera(Camera)}, the apps canuse the camera after this method
* call. The apps do not need to lock thecamera again. However, if this
* method fails, the apps should still lockthe camera back. The apps should
* not start another recording sessionduring recording.
*
* @throws IllegalStateException if itis called before
* prepare().
*/
public native void start() throws IllegalStateException;
在此解释下不少朋友问我,为何模拟器不可以运行的问题,手机上是没有问题的,我自己尝试过。请把录音类型设置为.3GP,因为有些手机可能对其他格式支持不是特别好。