-
大小: 10KB文件类型: .java金币: 1下载: 0 次发布日期: 2021-05-26
- 语言: Java
- 标签: AudioRecord
资源简介
网上的通话录音大多数都是基于mediarecoder开发的,我最后反编译了一款apk,发现里面用的是audiorecord开发的,下面是我的核心代码展示,良心开发,不要差评谢谢
代码片段和文件信息
package com.udows.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.onclickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity3 extends Activity {
// 音频获取源
private int audioSource = MediaRecorder.AudioSource.VOICE_RECOGNITION;
// 设置音频采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025
private static int sampleRateInHz = 44100;
// 设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
private static int channelConfig = AudioFormat.CHANNEL_IN_FRONT;
// 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
// 缓冲区字节大小
private int bufferSizeInBytes = 0;
private Button Start;
private Button Stop;
private AudioRecord audioRecord;
private boolean isRecord = false;// 设置正在录制的状态
// AudioName裸音频数据文件
private static final String AudioName = “/sdcard/love.raw“;
// NewAudioName可播放的音频文件
private static final String NewAudioName = “/sdcard/new.wav“;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);// 让界面横屏
requestWindowFeature(Window.FEATURE_NO_title);// 去掉界面标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 重新设置界面大小
setContentView(R.layout.activity_main);
init();
}
private void init() {
Start = (Button) this.findViewById(R.id.start);
Stop = (Button) this.findViewById(R.id.stop);
Start.setonclickListener(new TestAudioListener());
Stop.setonclickListener(new TestAudioListener());
creatAudioRecord();
}
private void creatAudioRecord() {
// 获得缓冲区字节大小
bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz
channelConfig audioFormat);
Log.e(“H3c“ “x:“ + bufferSizeInBytes);
// 创建AudioRecord对象
audioRecord = new AudioRecord(audioSource sampleRateInHz
channelConfig audioFormat bufferSizeInBytes*2);
}
class TestAudioListener implements onclickListener {
@Override
public void onclick(View v) {
if (v == Start) {
startRecord();
}
if (v == Stop) {
stopRecord();
}
}
}
private void startRecord() {
setMaxVoice();
audioRecord.startRe
评论
共有 条评论