gpt4 book ai didi

android - 录制的声音非常低,Android上录制了很多噪音

转载 作者:行者123 更新时间:2023-12-03 00:00:45 31 4
gpt4 key购买 nike

我正在尝试使用AudioRecord类录制语音并编写
使用AudioTrack类向扬声器读取的字节。我能听见
声音来自扬声器,但是声音很低,而且很多
声音随着录制的声音而来。

解决此问题的任何解决方案,以减少噪音和噪音
真实的声音。

我为此使用以下代码:

    package com.my.mic.record;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.util.Log;

public class Record extends Thread
{

int numCrossing,p,numSamples,af;

static final int bufferSize = 200000;
short[] buffer = new short[bufferSize];
short[] readBuffer = new short[bufferSize];
boolean isRecording;
AudioManager am;

public AudioRecord arec;
public AudioTrack atrack;

private int sampleRate = 8000;


public void run() {


isRecording = true;

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

int buffersize =
AudioRecord.getMinBufferSize(sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);

arec = new
AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT,
buffersize);

//buffersize = arec.getMinBufferSize(arec.getSampleRate(),
arec.getChannelConfiguration(), arec.getAudioFormat());

atrack = new
AudioTrack(AudioManager.STREAM_MUSIC,arec.getSampleRate(),arec.getChannelConfiguration(),arec.getAudioFormat(),
buffersize,AudioTrack.MODE_STREAM);

am.setRouting(AudioManager.MODE_NORMAL,AudioManager.ROUTE_EARPIECE,
AudioManager.ROUTE_ALL);
//am.setMode(AudioManager.MODE_NORMAL);


Log.d("SPEAKERPHONE", "Is speakerphone on? : " +
am.isSpeakerphoneOn());

atrack.setPlaybackRate(sampleRate);


byte[] buffer = new byte[buffersize];
arec.startRecording();
atrack.play();
//atrack.setStereoVolume(atrack.getMaxVolume(),
atrack.getMaxVolume());

final float frequency = sampleRate;
float increment = (float)((2*Math.PI) * frequency / 44100); //
angular increment for each sample
float angle = 0;
//AndroidAudioDevice device = new AndroidAudioDevice( );

while(isRecording) {

try {

arec.read(buffer, 0, buffersize);
atrack.write(buffer, 0, buffer.length);

} catch (Exception e) {
Log.d("Record", ""+e);
}
}

arec.stop();
atrack.stop();
//device.releaseTrack();
isRecording = false;
}


public boolean isRecording() {
return isRecording;
}

public void setRecording(boolean isRecording) {
this.isRecording = isRecording;
}

public AudioManager getAm() {
return am;
}

public void setAm(AudioManager am) {
this.am = am;
}

public void stopRecording(){
arec.stop();
arec.release();
atrack.stop();
atrack.release();
arec=null;
atrack=null;
setRecording(false);
this.stop();
}
}

最佳答案

转到this工作音频记录示例,如果您在模拟器中进行测试,则采样率将默认为8000。对于设备,将采样率提高到44100或更高并进行记录,它将更好地工作。

关于android - 录制的声音非常低,Android上录制了很多噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7442720/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com