Android audio方案,Android AudioRecord示例

b0b6db0cf529ef339db7dde3ece07537.png

慕神8447489

在这里,我向您发布一些代码示例,这些示例使用AudioRecord API记录了良好的声音质量。注意:如果在仿真器中使用,由于我们使用仅在仿真器中支持的8k采样率,因此音质不会很好。在设备中,使用采样率为44.1k以获得更好的质量。public class Audio_Record extends Activity {    private static final int RECORDER_SAMPLERATE = 8000;    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;    private AudioRecord recorder = null;    private Thread recordingThread = null;    private boolean isRecording = false;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        setButtonHandlers();        enableButtons(false);        int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,                RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);     }    private void setButtonHandlers() {        ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);        ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);    }    private void enableButton(int id, boolean isEnable) {        ((Button) findViewById(id)).setEnabled(isEnable);    }    private void enableButtons(boolean isRecording) {        enableButton(R.id.btnStart, !isRecording);        enableButton(R.id.btnStop, isRecording);    }    int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024    int BytesPerElement = 2; // 2 bytes in 16bit format    private void startRecording() {        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,                RECORDER_SAMPLERATE, RECORDER_CHANNELS,                RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);        recorder.startRecording();        isRecording = true;        recordingThread = new Thread(new Runnable() {            public void run() {                writeAudioDataToFile();            }        }, "AudioRecorder Thread");        recordingThread.start();    }        //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;    }    private void writeAudioDataToFile() {        // Write the output audio in byte        String filePath = "/sdcard/voice8K16bitmono.pcm";        short sData[] = new short[BufferElements2Rec];        FileOutputStream os = null;        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, BufferElements2Rec);            System.out.println("Short wirting to file" + sData.toString());            try {                // // writes the data to file from buffer                // // stores the voice buffer                byte bData[] = short2byte(sData);                os.write(bData, 0, BufferElements2Rec * BytesPerElement);            } catch (IOException e) {                e.printStackTrace();            }        }        try {            os.close();        } catch (IOException e) {            e.printStackTrace();        }    }    private void stopRecording() {        // stops the recording activity        if (null != recorder) {            isRecording = false;            recorder.stop();            recorder.release();            recorder = null;            recordingThread = null;        }    }    private View.OnClickListener btnClick = new View.OnClickListener() {        public void onClick(View v) {            switch (v.getId()) {            case R.id.btnStart: {                enableButtons(true);                startRecording();                break;            }            case R.id.btnStop: {                enableButtons(false);                stopRecording();                break;            }            }        }    };    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if (keyCode == KeyEvent.KEYCODE_BACK) {            finish();        }        return super.onKeyDown(keyCode, event);    }}有关更多详细信息,请尝试使用此AUDIORECORD博客。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值