Android實現(xiàn)邊錄邊播應用
本文實例為大家分享了Android實現(xiàn)邊錄邊播的具體代碼,供大家參考,具體內容如下
1.Android.mk
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-subdir-java-files) LOCAL_PACKAGE_NAME := testRecord include $(BUILD_PACKAGE)
2.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testRecord"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<application
android:icon="@drawable/icon"
android:label="Bug Report Sender">
<activity android:name=".testRecord"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
3.res/layout/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="@string/btnR"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="@string/btnS" android:id="@+id/btnStop"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/btnExit"
android:layout_width="fill_parent" android:text="@string/btnE"></Button>
<TextView android:id="@+id/TextView01" android:layout_height="wrap_content"
android:text="@string/textV" android:layout_width="fill_parent"></TextView>
<SeekBar android:layout_height="wrap_content" android:id="@+id/skbVolume"
android:layout_width="fill_parent"></SeekBar>
</LinearLayout>
4.res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">bianlubianbo</string>
<string name="btnR">start</string>
<string name="btnS">stop</string>
<string name="btnE">exit</string>
<string name="textV">vlounm</string>
</resources>
5.res/drawable/icom.png
6.src/com/testRecord/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;//調節(jié)音量
boolean isRecording = false;//是否錄放的標記
static final int frequency = 8000;//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*10);
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);//音量調節(jié)的極限
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保存數(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自定View流式布局根據(jù)文字數(shù)量換行
這篇文章主要為大家詳細介紹了Android自定View流式布局,根據(jù)文字數(shù)量換行,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
android ScrollView實現(xiàn)下拉放大頭部圖片
這篇文章主要為大家詳細介紹了android ScrollView實現(xiàn)下拉放大頭部圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android studio創(chuàng)建第一個app
這篇文章主要為大家詳細介紹了如何使用Android studio創(chuàng)建你的第一個項目Hello World,感興趣的小伙伴們可以參考一下2016-05-05
android如何獲取手機聯(lián)系人的數(shù)據(jù)庫示例代碼
很多人在做手機聯(lián)系人的apk時會遇到,如何獲取手機聯(lián)系人數(shù)據(jù)庫的問題,本篇文章主要介紹了android如何獲取手機聯(lián)系人的數(shù)據(jù)庫示例代碼,有興趣的可以了解一下。2017-01-01
Android 自定義布局豎向的ViewPager的實現(xiàn)
這篇文章主要介紹了Android 自定義布局豎向的ViewPager的實現(xiàn)的相關資料,需要的朋友可以參考下2017-05-05
Android實現(xiàn)SwipeRefreshLayout首次進入自動刷新
這篇文章主要為大家詳細介紹了Android實現(xiàn)SwipeRefreshLayout首次進入自動刷新,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

