欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android提高之AudioRecord實現(xiàn)助聽器的方法

 更新時間:2014年08月09日 09:19:39   投稿:shichen2014  
這篇文章主要介紹了Android中AudioRecord實現(xiàn)助聽器的方法,對進行Android項目開發(fā)有一定的借鑒價值,需要的朋友可以參考下

通常來說,在進行Android項目開發(fā)的時候可以通過MediaRecorder和AudioRecord這兩個工具來實現(xiàn)錄音的功能,MediaRecorder直接把麥克風(fēng)的數(shù)據(jù)存到文件,并且能夠直接進行編碼(如AMR,MP3等),而AudioRecord則是讀取麥克風(fēng)的音頻流。本文使用AudioRecord讀取音頻流,使用AudioTrack播放音頻流,通過“邊讀邊播放”以及增大音量的方式來實現(xiàn)一個簡單的助聽器程序。

此處需要注意:由于目前的Android模擬器還不支持AudioRecord,因此本程序需要編譯之后放到真機運行。

先貼出本文程序運行截圖:

另外還要注意:在本程序音量調(diào)節(jié)只是程序內(nèi)部調(diào)節(jié)音量而已,要調(diào)到最大音量還需要手動設(shè)置系統(tǒng)音量。

使用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="程序音量調(diào)節(jié)" 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;//調(diào)節(jié)音量
 boolean isRecording = false;//是否錄放的標(biāo)記
 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);//音量調(diào)節(jié)的極限
 skbVolume.setProgress(70);//設(shè)置seekbar的位置值
 audioTrack.setStereoVolume(0.7f, 0.7f);//設(shè)置當(dāng)前音量大小
 skbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
  float vol=(float)(seekBar.getProgress())/(float)(seekBar.getMax());
  audioTrack.setStereoVolume(vol, vol);//設(shè)置音量
  }
  
  @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保存數(shù)據(jù)到緩沖區(qū)
   int bufferReadResult = audioRecord.read(buffer, 0,
    recBufSize);

   byte[] tmpBuf = new byte[bufferReadResult];
   System.arraycopy(buffer, 0, tmpBuf, 0, bufferReadResult);
   //寫入數(shù)據(jù)即播放
   audioTrack.write(tmpBuf, 0, tmpBuf.length);
  }
  audioTrack.stop();
  audioRecord.stop();
  } catch (Throwable t) {
  Toast.makeText(testRecord.this, t.getMessage(), 1000);
  }
 }
 };
}

希望本文所述實例對大家的Android項目開發(fā)有一定的借鑒價值。

相關(guān)文章

  • Android自定義控件實現(xiàn)通用驗證碼輸入框(二)

    Android自定義控件實現(xiàn)通用驗證碼輸入框(二)

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件實現(xiàn)通用驗證碼輸入框的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • android實現(xiàn)多點觸摸效果

    android實現(xiàn)多點觸摸效果

    這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)多點觸摸效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android Studio 4.0 新功能中的Live Layout Inspector詳解

    Android Studio 4.0 新功能中的Live Layout Inspector詳解

    這篇文章主要介紹了Android Studio 4.0 新功能中的Live Layout Inspector,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 簡單學(xué)習(xí)Android TextView

    簡單學(xué)習(xí)Android TextView

    這篇文章主要和大家一起簡單學(xué)習(xí)Android TextView,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Android 矩陣ColorMatrix

    Android 矩陣ColorMatrix

    這篇文章主要介紹了Android 矩陣ColorMatrix的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • App中如何獲取gradle的配置信息

    App中如何獲取gradle的配置信息

    這篇文章主要給大家介紹了關(guān)于App中如何獲取gradle的配置信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • 詳解如何實現(xiàn)一個Kotlin函數(shù)類型

    詳解如何實現(xiàn)一個Kotlin函數(shù)類型

    這篇文章主要為大家詳細(xì)介紹了如何實現(xiàn)一個Kotlin函數(shù)類型,文中的實現(xiàn)方法講解詳細(xì),具有一定的借鑒價值,需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-10-10
  • 基于Android實現(xiàn)煙花效果

    基于Android實現(xiàn)煙花效果

    這篇文章文章我們將介紹如何通過Canvas 2D坐標(biāo)系實現(xiàn)粒子效果,文中通過代碼示例和圖文結(jié)合介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以自己動手嘗試一下
    2023-11-11
  • Android中的Parcelable序列化對象

    Android中的Parcelable序列化對象

    這篇文章主要介紹了Android中的Parcelable序列化對象,需要的朋友可以參考下
    2016-01-01
  • Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫實例詳解

    Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫實例詳解

    這篇文章主要介紹了Android編程操作嵌入式關(guān)系型SQLite數(shù)據(jù)庫的方法,結(jié)合實例形式較為詳細(xì)的分析了Android操作SQLite數(shù)據(jù)庫的基本技巧與相關(guān)注意事項,需要的朋友可以參考下
    2016-01-01

最新評論