如题,即听筒和话筒的声音,我用了设置录音源为AudioSource.MIC 可是只有话筒的声音。 和 VOICE_CALL 很多厂商不支持 求大神指点!最好给出代码,小弟感激不尽
1条回答 默认 最新
- 飞火流星02027 2017-07-05 07:59关注
import java.io.File;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {
//存储很多张话筒图片的数组
private Drawable[] micImages;
//话筒的图片
private ImageView micImage;private RelativeLayout recordingContainer; private TextView recordingHint; private Button buttonPressToSpeak; private MediaRecorder recorder=null; //录音存储在SD卡的路径 private String output_Path=Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator+"luyin.3gp"; //录音文件 private File soundFile; private int BASE = 600; private int SPACE = 200;// 间隔取样时间 private final Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { int what=msg.what; //根据mHandler发送what的大小决定话筒的图片是哪一张 //说话声音越大,发送过来what值越大 if(what>13){ what=13; } micImage.setImageDrawable(micImages[what]); }; }; private Runnable mUpdateMicStatusTimer = new Runnable() { public void run() { updateMicStatus(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { micImage=(ImageView) findViewById(R.id.mic_image); recordingHint=(TextView) findViewById(R.id.recording_hint); recordingContainer=(RelativeLayout) findViewById(R.id.recording_container); buttonPressToSpeak=(Button) findViewById(R.id.buttonPressToSpeak); buttonPressToSpeak.setOnTouchListener(new PressToSpeakListen()); // 动画资源文件,用于录制语音时 micImages = new Drawable[] { getResources().getDrawable(R.drawable.record_animate_01), getResources().getDrawable(R.drawable.record_animate_02), getResources().getDrawable(R.drawable.record_animate_03), getResources().getDrawable(R.drawable.record_animate_04), getResources().getDrawable(R.drawable.record_animate_05), getResources().getDrawable(R.drawable.record_animate_06), getResources().getDrawable(R.drawable.record_animate_07), getResources().getDrawable(R.drawable.record_animate_08), getResources().getDrawable(R.drawable.record_animate_09), getResources().getDrawable(R.drawable.record_animate_10), getResources().getDrawable(R.drawable.record_animate_11), getResources().getDrawable(R.drawable.record_animate_12), getResources().getDrawable(R.drawable.record_animate_13), getResources().getDrawable(R.drawable.record_animate_14), }; } /** * 按住说话listener * */ class PressToSpeakListen implements View.OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (!isExitsSdcard()) { Toast.makeText(MainActivity.this, "未检测到SD卡", Toast.LENGTH_SHORT).show(); return false; } if(recorder!=null){ recorder.stop(); recorder.release(); recorder=null; } soundFile=new File(output_Path); recorder=new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//声音来源是话筒 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置格式 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置解码方式 recorder.setOutputFile(soundFile.getAbsolutePath()); try { recorder.prepare(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } recorder.start(); updateMicStatus(); recordingContainer.setVisibility(View.VISIBLE); recordingHint.setText("手指上滑,取消发送"); recordingHint.setBackgroundColor(Color.TRANSPARENT); return true; case MotionEvent.ACTION_MOVE: { //在这里只是做了监听,并没有做与发送相关的处理 if (event.getY() < 0) { recordingHint.setText("松开手指,取消发送"); recordingHint.setBackgroundResource(R.drawable.recording_text_hint_bg); } else { recordingHint.setText("手指上滑,取消发送"); recordingHint.setBackgroundColor(Color.TRANSPARENT); } return true; } case MotionEvent.ACTION_UP: //抬起手指,停止录音 recordingContainer.setVisibility(View.INVISIBLE); stopRecoder(); Toast.makeText(getApplicationContext(), "录音存储到了"+soundFile.getAbsolutePath(), 1).show(); return true; default: return false; } } } private void stopRecoder(){ if (soundFile != null && soundFile.exists()) { // 停止录音 recorder.stop(); // 释放资源 recorder.release(); recorder = null; } } private void updateMicStatus() { if(recorder!=null){ int ratio = recorder.getMaxAmplitude() / BASE; int db = 0;// 分贝 if (ratio > 1) db = (int) (20 * Math.log10(ratio)); System.out.println("分贝值:"+db+" "+Math.log10(ratio)); //我对着手机说话声音最大的时候,db达到了35左右, mHandler.postDelayed(mUpdateMicStatusTimer, SPACE); //所以除了2,为的就是对应14张图片 mHandler.sendEmptyMessage(db/2); } } /** * 检测Sdcard是否存在 * * @return */ public boolean isExitsSdcard() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) return true; else return false; }
}
解决 无用评论 打赏 举报
悬赏问题
- ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
- ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
- ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
- ¥15 乘性高斯噪声在深度学习网络中的应用
- ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
- ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
- ¥15 C++ 头文件/宏冲突问题解决
- ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
- ¥50 安卓adb backup备份子用户应用数据失败
- ¥20 有人能用聚类分析帮我分析一下文本内容嘛