录制的音频不清楚

时间:2014-03-09 17:05:48

标签: android audio

我正在使用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;
}

其中rate = 44100,channel config = 16,buffer size = 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();
    }
}

}

2 个答案:

答案 0 :(得分:0)

试试这段代码。它有效。

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();
            }
        });
    }
}

答案 1 :(得分:0)

尝试为您的 MediaRecorder

设置这些值
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION );
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setAudioEncodingBitRate(16);
mediaRecorder.setAudioSamplingRate(44100);

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