Android提高第十篇之AudioRecord實現 助聽器

來源:互聯網
上載者:User

Android可以通過MediaRecorder和AudioRecord這兩個工具來實現錄音,MediaRecorder直接把麥克風的資料存到檔案,並且能夠直接進行編碼(如AMR,MP3等),而AudioRecord則是讀取麥克風的音頻流。本文使用AudioRecord讀取音頻流,使用AudioTrack播放音頻流,通過“邊讀邊播放”以及增大音量的方式來實現一個簡單的助聽器程式。

PS:由於目前的Android模擬器還不支援AudioRecord,因此本程式需要編譯之後放到真機運行。

先貼出本文程式運行:


PS:程式音量大小只是程式內部調節音量而已,要調到最大音量還需要手動設定系統音量。

 


使用AudioRecord必須要申請許可,在AndroidManifest.xml裡面添加這句:

view plaincopy to clipboardprint?
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> 
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

main.xml的源碼如下:

view plaincopy to clipboardprint?
<?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> 
<?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的源碼如下:

view plaincopy to clipboardprint?
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());  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.