当前位置: 代码迷 >> Android >> 录制的音频不清楚
  详细解决方案

录制的音频不清楚

热度:41   发布时间:2023-08-04 12:27:53.0

我正在使用AudioRecorder在Android中录制音频,但我遇到了录制歌曲的问题,但是音调和频率运行太快,导致声音变差。 我怎样才能解决这个问题 ?

// convert short to byte
private byte[] short2byte(short[] sData) {
    int shortArrsize = sData.length;
    byte[] bytes = new byte[shortArrsize * 2];
    for (int i = 0; i < shortArrsize; i++) {
        bytes[i * 2] = (byte) (sData[i] & 0x00FF);
        bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);
        sData[i] = 0;
    }
    return bytes;

}

public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        for (short audioFormat : new short[]{AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT}) {
            for (short channelConfig : new short[]{AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO}) {
                try {
                    Log.d(LOG_TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
                            + channelConfig);
                    bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        // check if we can instantiate and have a success
                        recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);
                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
                            return recorder;
                    }
                } catch (Exception e) {
                    Log.e(LOG_TAG, rate + "Exception, keep trying.", e);
                }
            }
        }
    }
    return null;
}

其中速率= 44100,通道配置= 16,缓冲区大小= 4096,PCM_ENCODING_16_BIT

private void writeAudioDataToFile() {
// Write the output audio in byte
String filePath = "/sdcard/8k16bitMono.wav";

short sData[] = new short[bufferSize / 2];

try {
    os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
while (isRecording) {
    // gets the voice output from microphone to byte format
    recorder.read(sData, 0, bufferSize / 2);
    System.out.println("Short writing to file" + sData.toString());
    try {
        // writes the data to file from buffer stores the voice buffer
        byte bData[] = short2byte(sData);

        os.write(bData, 0, bufferSize);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

试试这个代码。 有用。

public class MainActivity extends Activity{
    Button play,stop,record;

    private MediaRecorder myAudioRecorder;
    private String outputFile = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play=(Button)findViewById(R.id.button3);
        stop=(Button)findViewById(R.id.button2);
        record=(Button)findViewById(R.id.button);

        stop.setEnabled(false);
        play.setEnabled(false);
        outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";;

        myAudioRecorder = new MediaRecorder();
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myAudioRecorder.setOutputFile(outputFile);

        record.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                try {
                    myAudioRecorder.prepare();
                    myAudioRecorder.start();    

                }catch (IllegalStateException e) {

                    e.printStackTrace();


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                record.setEnabled(false);
                stop.setEnabled(true);


                Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
            }
        });


        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myAudioRecorder.stop();
                myAudioRecorder.release();
                myAudioRecorder  = null;

                stop.setEnabled(false);
                play.setEnabled(true);

                Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
            }
        });

        play.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                MediaPlayer m = new MediaPlayer();

                try {
                    m.setDataSource(outputFile); 
                }catch (IOException e){

                    e.printStackTrace();                
                }

                try {

                    m.prepare();                    

                } catch (IOException e){
                    e.printStackTrace();
                }


                m.start();

                Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
            }
        });
    }
}

尝试为MediaRecorder设置这些值

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION );
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setAudioEncodingBitRate(16);
mediaRecorder.setAudioSamplingRate(44100);

这种使我的音频质量更好!

  相关解决方案