本文转载自:http://blog.csdn.net/fnuwfnh/article/details/46698509

最近在学习android 录音方面的知识,发现在部分手机正常运行的APP,在华为平板上挂了,eclipse的Logcat显示MediaRecorder start failed:-38。查了下资料,原因是我的APP在编码时使用了多路录音,而调试用的华为平板在硬件上只能单路录音,不支持多路录音。目前mtk的75,15,77,17平台硬件上只支持单路录音,不支持多路录音,后续89平台在这块已经做了改进,多路录音需要硬件支持,后续更高阶的平台是可以支持多路录音的。PDXXXX是mtk77平台,硬件上只支持单路录音,不支持多路录音。

解决的办法就是优化代码,看能不能从代码上调整为单路录音了。 
以下是我APP中两处用到MIC SOURCE之处,分别用来实现边录音边播放以及保存录音文件。在支持多路录音的手机上正常运行,而在不支持多路录音的平台上,通过改代码,测试发现要么只能边录音边播放,要么只能保存录音文件。要想保存录音文件,必须得在start前(需要一段时间延迟)释放边录音边播放占用的MIC SOURCE。

AudioRecord audioRecord2 = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, recBufSize); AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,
channelConfiguration, audioEncoding,
playBufSize, AudioTrack.MODE_STREAM);
new RecordPlayThread().start(); class RecordPlayThread extends Thread {
public void run() {
try {
byte[] buffer = new byte[recBufSize2];
audioRecord2.startRecording();//开始录制
audioTrack.play();//开始播放 while (isRecording) {
//从MIC保存数据到缓冲区
int bufferReadResult = audioRecord2.read(buffer, 0, recBufSize2);
byte[] tmpBuf = new byte[bufferReadResult];
System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
//写入数据即播放
for (int i=0; i<buffer.length; i++){
tmpBuf[i] = (byte) (tmpBuf[i]*2);
}
audioTrack.write(tmpBuf, 0, tmpBuf.length);
}
audioTrack.stop();
audioTrack.release();
audioTrack = null;
audioRecord2.stop();
audioRecord2.release();
audioRecord2 = null;
} catch (Throwable t) {
Toast.makeText(testOscilloscope.this, t.getMessage(), 1000);
}
}
}
//释放底层资源
//mRecorder.stop();
//mRecorder.release();
//mRecorder = null;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
MediaRecorder mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(Second_Path+date+".amr");
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
//释放底层资源
//mRecorder.stop();
//mRecorder.release();
//mRecorder = null;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

记得释放底层资源~否则会导致其它APP无法得到底层的录音资源,从而无法录音,导致运行时异常。

android MediaRecorder start failed:-38【转】的更多相关文章

  1. Android: MediaRecorder start failed

    在某些机型上,MediaRecorder在调用start方法时,会出现start failed的错误,有一种可能是setVideoFrameRate导致的.要解决这个问题,只需要注释掉这条语句就可以了 ...

  2. I.MX6 Android iperf3 porting failed

    /***************************************************************************** * I.MX6 Android iperf ...

  3. Android resource compilation failed

    报错:Android resource compilation failed D:\android\EasySports\app\build\intermediates\incremental\mer ...

  4. Android MediaRecorder解析

    源码路径:frameworks/base/media/java/android/media/MediaRecorder.javaframeworks/base/media/jni/android_me ...

  5. org.jetbrains.android.uipreview.RenderingException: Failed to load the LayoutLib: com/android/layoutlib/bridge/Bridge : Unsupported major.minor version 52.0

    在Android Studio使用的时候,突然发现Preview功能不能用了,报了一个错,错误如下 org.jetbrains.android.uipreview.RenderingException ...

  6. Android MediaRecorder自定义分辨率

    Android MediaRecorder自定义分辨率 工作这么久了,确实积累了不少东西,但都是以文档的形式存在U盘里的,为什么不写博客呢?因为懒啊!!!总感觉博客太难写了(大概是上学时候写作文恐惧症 ...

  7. Android Studio:Failed to resolve ***

    更换电脑后,也更新了所有的SDK的tool,仍然报错:Failed to resolve  各种jar包,出现这种问题主要是因为在Android studio中默认不允许在线更新,修改方法如下:

  8. Android Studio Problem : failed to find style 'textviewstyle' in current theme 解决方法

    新建一个空白的MainActivity时Preview就出现一个错误: failed to find style 'textviewstyle' in current theme 开始在国内的博客平台 ...

  9. Android MediaRecorder录制音频

    今天介绍一下在Android中怎么录制音频,在Android中使用MediaRecorder来录制音频,步骤: 1.创建MediaRecorder对象. 2.调用MediaRecorder对象的set ...

随机推荐

  1. react-native 0.58版本打包图片问题 task ':app:mergeReleaseResources' Error: Duplicate resources

    debug没问题,在生成正式apk的时候就如下: google了一下在github上找到了解决方案: github问题指向 在node_modules/react-native/react.gradl ...

  2. [Python3网络爬虫开发实战] 4.1-使用XPath

    XPath,全称XML Path Language,即XML路径语言,它是一门在XML文档中查找信息的语言.它最初是用来搜寻XML文档的,但是它同样适用于HTML文档的搜索. 所以在做爬虫时,我们完全 ...

  3. ubuntu14.04 mysql-workbench Connecting to MySQL server ... Native table 'performance_schema'.'session_variables' has the wrong structure错误解决

    使用的mysql版本: mysql  Ver 14.14 Distrib 5.7.9, for Linux (x86_64) using  EditLine wrapper 打开shell命令 1.输 ...

  4. uva 10596 - Morning Walk

    Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chitta ...

  5. vue-router 根据路由动态添加目录 控制目录权限

    <template> <el-row class="el-menu" > <el-menu router :default-active='$rout ...

  6. 洛谷 2187 小Z的笔记

    [题解] DP.  设f[i]表示前i个字母,保留第i个字母,最多可以保留多少个字母:设g[i]为当前字母为i的位置对应的f的最大值. 转移方程就是f[i]=max(f[i], g[j]+1) (j与 ...

  7. Mvc Action可以通过jsonp方式调取

    jsonp其实是一种特殊的数据获取格式,所以在Aicton直接调取的时候肯定会出现问题,下面代码是对于jsonp调取做的处理 protected virtual ActionResult Create ...

  8. node.js 读取文件--createReadStream

    createReadStream 是fs模块里面读流的一个方法 这个方法基于fs模块的,所以我们先要引进fs模块 let fs=require("fs"); createReadS ...

  9. JSP处理XML数据

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/xml-data.html: 当通过HTTP发送XML数据时,使用JSP处理传入和传出的XML文件是有意义 ...

  10. 加上mvc:resources后controller访问404

    之前因为静态资源访问,404,于是加上了类似的代码 <mvc:resources location="/resources/" mapping="/resource ...