gpt4 book ai didi

android - Android:播放PCM文件时出错

转载 作者:行者123 更新时间:2023-12-03 02:33:59 25 4
gpt4 key购买 nike

我正在尝试播放从麦克风录制音频时创建的PCM文件。

我使用AudioRecord是因为我想分析麦克风记录的声音的频率。

播放PCM文件的代码段如下。但是,当我尝试播放文件时,正在播放很大的声音。

FileInputStream fis=null;
File l=null;
l=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Notate/f1.pcm");

byte[] buffer=new byte[(int)l.length()];

try {
fis = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Notate/f1.pcm");
fis.read(buffer);
}
catch(Exception e)
{

}
int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);
if (at!=null) {
at.play();
at.write(buffer, 0, buffer.length);
at.stop();
at.release();
}

下面是用于存储PCM文件的代码。
private class RecordAudio extends AsyncTask<Void, Double, Void> {
FileOutputStream os = null;
BufferedOutputStream bos =null;
DataOutputStream dos = null;
@Override
protected Void doInBackground(Void... params) {
int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioEncoding); // Gets the minimum buffer needed
AudioRecord audioRecord = new AudioRecord(audioSource, sampleRate, channelConfig, audioEncoding, bufferSize); // The RAW PCM sample recording
File f=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Notate/");
if(!f.isDirectory()) {
File newDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Notate/");
newDirectory.mkdirs();
}
String filepath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Notate/f1.pcm";
short[] buffer = new short[blockSize]; // Save the raw PCM samples as short bytes
try{
os=new FileOutputStream(filepath);
}
catch(FileNotFoundException e)
{

}
bos = new BufferedOutputStream(os);
dos = new DataOutputStream(bos);
try {
audioRecord.startRecording(); //Start
} catch (Throwable t) {
}
while (started) {
int length=audioRecord.read(buffer, 0, blockSize);

Yin alpha = new Yin(44100, 1024, 0.2);
float[] floaters = new float[buffer.length];
for (int i = 0; i < buffer.length; i++) {
floaters[i] = buffer[i];
}
for(int k=0;k<length;k++) {
try {
dos.writeShort(buffer[k]);


} catch (IOException e) {

}
}
float result = alpha.getPitch(floaters);
publishProgress((double) result);

}
try{
dos.close();
}
catch(IOException e){
e.printStackTrace();
}

return null;
}

最佳答案

writeShort以大尾数格式(即最高有效字节在前)写入值,而AudioTrack可能期望样本为小尾数(最低有效字节在前)。

如果在录制时使用byte[]而不是short[],则会使事情变得更简单。然后,您可以简单地使用DataOutputStream写入dos.write(buffer, 0, length);。在这种情况下,您必须对转换为浮点值进行一些修改:

for (int i = 0; i < length; i += 2) {
floaters[i] = (short)(buffer[i] | (buffer[i+1] << 8));
}

关于android - Android:播放PCM文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27778336/

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