Android提高第十篇之AudioRecord实现"助听器"

转载以后有用,感谢原作者。

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

       Android可以通过MediaRecorder和AudioRecord这两个工具来实现录音,MediaRecorder直接把麦克风的数据存到文件,并且能够直接进行编码(如AMR,MP3等),而AudioRecord则是读取麦克风的音频流。本文使用AudioRecord读取音频流,使用AudioTrack播放音频流,通过“边读边播放”以及增大音量的方式来实现一个简单的助听器程序。

PS:由于目前的Android模拟器还不支持AudioRecord,因此本程序需要编译之后放到真机运行。

先贴出本文程序运行截图:


PS:程序音量调节只是程序内部调节音量而已,要调到最大音量还需要手动设置系统音量。

使用AudioRecord必须要申请许可,在AndroidManifest.xml里面添加这句:

<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>  
main.xml的源码如下:
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
  
    <Button android:layout_height="wrap_content" android:id="@+id/btnRecord"  
        android:layout_width="fill_parent" android:text="开始边录边放"></Button>  
    <Button android:layout_height="wrap_content"  
        android:layout_width="fill_parent" android:text="停止" android:id="@+id/btnStop"></Button>  
    <Button android:layout_height="wrap_content" android:id="@+id/btnExit"  
        android:layout_width="fill_parent" android:text="退出"></Button>  
    <TextView android:id="@+id/TextView01" android:layout_height="wrap_content"  
        android:text="程序音量调节" android:layout_width="fill_parent"></TextView>  
    <SeekBar android:layout_height="wrap_content" android:id="@+id/skbVolume"  
        android:layout_width="fill_parent"></SeekBar>  
  
</LinearLayout>  
testRecord.java的源码如下:

package com.testRecord;  
  
import android.app.Activity;  
import android.media.AudioFormat;  
import android.media.AudioManager;  
import android.media.AudioRecord;  
import android.media.AudioTrack;  
import android.media.MediaRecorder;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.SeekBar;  
import android.widget.Toast;  
  
public class testRecord extends Activity {  
    /** Called when the activity is first created. */  
    Button btnRecord, btnStop, btnExit;  
    SeekBar skbVolume;//调节音量  
    boolean isRecording = false;//是否录放的标记  
    static final int frequency = 44100;  
    static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;  
    static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;  
    int recBufSize,playBufSize;  
    AudioRecord audioRecord;  
    AudioTrack audioTrack;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        setTitle("助听器");  
        recBufSize = AudioRecord.getMinBufferSize(frequency,  
                channelConfiguration, audioEncoding);  
  
        playBufSize=AudioTrack.getMinBufferSize(frequency,  
                channelConfiguration, audioEncoding);  
        // -----------------------------------------  
        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency,  
                channelConfiguration, audioEncoding, recBufSize);  
  
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,  
                channelConfiguration, audioEncoding,  
                playBufSize, AudioTrack.MODE_STREAM);  
        //------------------------------------------  
        btnRecord = (Button) this.findViewById(R.id.btnRecord);  
        btnRecord.setOnClickListener(new ClickEvent());  
        btnStop = (Button) this.findViewById(R.id.btnStop);  
        btnStop.setOnClickListener(new ClickEvent());  
        btnExit = (Button) this.findViewById(R.id.btnExit);  
        btnExit.setOnClickListener(new ClickEvent());  
        skbVolume=(SeekBar)this.findViewById(R.id.skbVolume);  
        skbVolume.setMax(100);//音量调节的极限  
        skbVolume.setProgress(70);//设置seekbar的位置值  
        audioTrack.setStereoVolume(0.7f, 0.7f);//设置当前音量大小  
        skbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {  
              
            @Override  
            public void onStopTrackingTouch(SeekBar seekBar) {  
                float vol=(float)(seekBar.getProgress())/(float)(seekBar.getMax());  
                audioTrack.setStereoVolume(vol, vol);//设置音量  
            }  
              
            @Override  
            public void onStartTrackingTouch(SeekBar seekBar) {  
                // TODO Auto-generated method stub  
            }  
              
            @Override  
            public void onProgressChanged(SeekBar seekBar, int progress,  
                    boolean fromUser) {  
                // TODO Auto-generated method stub  
            }  
        });  
    }  
  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        android.os.Process.killProcess(android.os.Process.myPid());  
    }  
  
    class ClickEvent implements View.OnClickListener {  
  
        @Override  
        public void onClick(View v) {  
            if (v == btnRecord) {  
                isRecording = true;  
                new RecordPlayThread().start();// 开一条线程边录边放  
            } else if (v == btnStop) {  
                isRecording = false;  
            } else if (v == btnExit) {  
                isRecording = false;  
                testRecord.this.finish();  
            }  
        }  
    }  
  
    class RecordPlayThread extends Thread {  
        public void run() {  
            try {  
                byte[] buffer = new byte[recBufSize];  
                audioRecord.startRecording();//开始录制  
                audioTrack.play();//开始播放  
                  
                while (isRecording) {  
                    //从MIC保存数据到缓冲区  
                    int bufferReadResult = audioRecord.read(buffer, 0,  
                            recBufSize);  
  
                    byte[] tmpBuf = new byte[bufferReadResult];  
                    System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);  
                    //写入数据即播放  
                    audioTrack.write(tmpBuf, 0, tmpBuf.length);  
                }  
                audioTrack.stop();  
                audioRecord.stop();  
            } catch (Throwable t) {  
                Toast.makeText(testRecord.this, t.getMessage(), 1000);  
            }  
        }  
    };  
}  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 Android 音频频谱图需要用到 Android 的 `AudioRecord` 类和 `FFT`(快速傅里叶变换)算法。下面是一个简单的示例代码: ```java import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Arrays; public class MainActivity extends AppCompatActivity { private static final String TAG = "AudioRecord"; private static final int SAMPLE_RATE = 44100; // 采样率 private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO; // 声道 private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT; // 采样精度 private static final int BUFFER_SIZE = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT); // 缓冲区大小 private static final int MSG_UPDATE_VIEW = 1; // 更新 UI 的消息 private AudioRecord mAudioRecord; private boolean mIsRecording; private Handler mHandler; private FFT mFft; private TextView mTextView; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = findViewById(R.id.text_view); mButton = findViewById(R.id.button); mFft = new FFT(BUFFER_SIZE); mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_UPDATE_VIEW: updateView((double[]) msg.obj); break; } } }; } public void onClick(View view) { if (mIsRecording) { stopRecording(); } else { startRecording(); } } private void startRecording() { mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE); mAudioRecord.startRecording(); mIsRecording = true; mButton.setText(R.string.stop_recording); new Thread(new Runnable() { @Override public void run() { short[] buffer = new short[BUFFER_SIZE]; double[] fftBuffer = new double[BUFFER_SIZE]; while (mIsRecording) { int readSize = mAudioRecord.read(buffer, 0, BUFFER_SIZE); for (int i = 0; i < readSize; i++) { fftBuffer[i] = buffer[i] / 32768.0; } mFft.fft(fftBuffer); double[] spectrum = new double[BUFFER_SIZE / 2]; for (int i = 0; i < spectrum.length; i++) { double re = fftBuffer[i * 2]; double im = fftBuffer[i * 2 + 1]; spectrum[i] = Math.sqrt(re * re + im * im); } Message msg = mHandler.obtainMessage(MSG_UPDATE_VIEW, spectrum); mHandler.sendMessage(msg); } } }).start(); } private void stopRecording() { mIsRecording = false; mButton.setText(R.string.start_recording); mAudioRecord.stop(); mAudioRecord.release(); mAudioRecord = null; } private void updateView(double[] spectrum) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < spectrum.length; i++) { sb.append(String.format("%d Hz: %.2f\n", i * SAMPLE_RATE / BUFFER_SIZE, spectrum[i])); } mTextView.setText(sb.toString()); } private static class FFT { private int n; private double[] cosTable; private double[] sinTable; FFT(int n) { this.n = n; this.cosTable = new double[n / 2]; this.sinTable = new double[n / 2]; for (int i = 0; i < n / 2; i++) { cosTable[i] = Math.cos(-2 * Math.PI * i / n); sinTable[i] = Math.sin(-2 * Math.PI * i / n); } } void fft(double[] data) { int bits = (int) (Math.log(n) / Math.log(2)); for (int j = 1; j < n / 2; j++) { int swapPos = reverseBits(j, bits); double temp = data[j]; data[j] = data[swapPos]; data[swapPos] = temp; } int size = 2; int halfSize = 1; for (int i = 1; i <= bits; i++) { for (int j = 0; j < n; j += size) { for (int k = 0; k < halfSize; k++) { int evenIndex = j + k; int oddIndex = j + k + halfSize; double even = data[evenIndex]; double odd = data[oddIndex]; double cos = cosTable[k * n / size]; double sin = sinTable[k * n / size]; data[evenIndex] = even + odd * cos - sin * odd; data[oddIndex] = even + odd * cos + sin * odd; } } size *= 2; halfSize *= 2; } } private int reverseBits(int x, int bits) { int y = 0; for (int i = 0; i < bits; i++) { y <<= 1; y |= (x & 1); x >>= 1; } return y; } } } ``` 上面的代码实现了一个简单的录音和频谱图显示功能。当用户点击“开始录音”按钮时,程序会创建一个 `AudioRecord` 对象并开始录音。录音过程中,程序会不断从缓冲区读取音频数据,并计算出其频谱。频谱计算使用了 FFT 算法,该算法可以将时域信号转换为频域信号。最后,程序将计算出的频谱显示在界面上。 需要注意的是,程序中使用了一个自己实现的 FFT 类。如果你对 FFT 算法不熟悉,可以先了解一下 FFT 算法的基本原理,再来理解这段代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值