




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android怎么實(shí)現(xiàn)錄音功能
這篇文章將為大家詳細(xì)講解有關(guān)Android怎么實(shí)現(xiàn)錄音功能,在下覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。效果圖如下所示:使用方法:錄音工具類:AudioRecoderUtils.java,代碼如下:public
class
AudioRecoderUtils
{
//文件路徑
private
String
filePath;
//文件夾路徑
private
String
FolderPath;
private
MediaRecorder
mMediaRecorder;
private
final
String
TAG
=
"fan";
public
static
final
int
MAX_LENGTH
=
1000
*
60
*
10;//
最大錄音時(shí)長1000*60*10;
private
OnAudioStatusUpdateListener
audioStatusUpdateListener;
/**
*
文件存儲默認(rèn)sdcard/record
*/
public
AudioRecoderUtils(){
//默認(rèn)保存路徑為/sdcard/record/下
this(Environment.getExternalStorageDirectory()+"/record/");
}
public
AudioRecoderUtils(String
filePath)
{
File
path
=
new
File(filePath);
if(!path.exists())
path.mkdirs();
this.FolderPath
=
filePath;
}
private
long
startTime;
private
long
endTime;
/**
*
開始錄音
使用amr格式
*
錄音文件
*
@return
*/
public
void
startRecord()
{
//
開始錄音
/*
①Initial:實(shí)例化MediaRecorder對象
*/
if
(mMediaRecorder
==
null)
mMediaRecorder
=
new
MediaRecorder();
try
{
/*
②setAudioSource/setVedioSource
*/
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//
設(shè)置麥克風(fēng)
/*
②設(shè)置音頻文件的編碼:AAC/AMR_NB/AMR_MB/Default
聲音的(波形)的采樣
*/
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
/*
*
②設(shè)置輸出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default
THREE_GPP(3gp格式
*
,H263視頻/ARM音頻編碼)、MPEG-4、RAW_AMR(只支持音頻且音頻編碼要求為AMR_NB)
*/
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
filePath
=
FolderPath
+
TimeUtils.getCurrentTime()
+
".amr"
;
/*
③準(zhǔn)備
*/
mMediaRecorder.setOutputFile(filePath);
mMediaRecorder.setMaxDuration(MAX_LENGTH);
mMediaRecorder.prepare();
/*
④開始
*/
mMediaRecorder.start();
//
AudioRecord
audioRecord.
/*
獲取開始時(shí)間*
*/
startTime
=
System.currentTimeMillis();
updateMicStatus();
Log.e("fan",
"startTime"
+
startTime);
}
catch
(IllegalStateException
e)
{
Log.i(TAG,
"call
startAmr(File
mRecAudioFile)
failed!"
+
e.getMessage());
}
catch
(IOException
e)
{
Log.i(TAG,
"call
startAmr(File
mRecAudioFile)
failed!"
+
e.getMessage());
}
}
/**
*
停止錄音
*/
public
long
stopRecord()
{
if
(mMediaRecorder
==
null)
return
0L;
endTime
=
System.currentTimeMillis();
//有一些網(wǎng)友反應(yīng)在5.0以上在調(diào)用stop的時(shí)候會報(bào)錯(cuò),翻閱了一下谷歌文檔發(fā)現(xiàn)上面確實(shí)寫的有可能會報(bào)錯(cuò)的情況,捕獲異常清理一下就行了,感謝大家反饋!
try
{
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder
=
null;
audioStatusUpdateListener.onStop(filePath);
filePath
=
"";
}catch
(RuntimeException
e){
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder
=
null;
File
file
=
new
File(filePath);
if
(file.exists())
file.delete();
filePath
=
"";
}
return
endTime
-
startTime;
}
/**
*
取消錄音
*/
public
void
cancelRecord(){
try
{
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder
=
null;
}catch
(RuntimeException
e){
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder
=
null;
}
File
file
=
new
File(filePath);
if
(file.exists())
file.delete();
filePath
=
"";
}
private
final
Handler
mHandler
=
new
Handler();
private
Runnable
mUpdateMicStatusTimer
=
new
Runnable()
{
public
void
run()
{
updateMicStatus();
}
};
private
int
BASE
=
1;
private
int
SPACE
=
100;//
間隔取樣時(shí)間
public
void
setOnAudioStatusUpdateListener(OnAudioStatusUpdateListener
audioStatusUpdateListener)
{
this.audioStatusUpdateListener
=
audioStatusUpdateListener;
}
/**
*
更新麥克狀態(tài)
*/
private
void
updateMicStatus()
{
if
(mMediaRecorder
!=
null)
{
double
ratio
=
(double)mMediaRecorder.getMaxAmplitude()
/
BASE;
double
db
=
0;//
分貝
if
(ratio
>
1)
{
db
=
20
*
Math.log10(ratio);
if(null
!=
audioStatusUpdateListener)
{
audioStatusUpdateListener.onUpdate(db,System.currentTimeMillis()-startTime);
}
}
mHandler.postDelayed(mUpdateMicStatusTimer,
SPACE);
}
}
public
interface
OnAudioStatusUpdateListener
{
/**
*
錄音中...
*
@param
db
當(dāng)前聲音分貝
*
@param
time
錄音時(shí)長
*/
public
void
onUpdate(double
db,long
time);
/**
*
停止錄音
*
@param
filePath
保存路徑
*/
public
void
onStop(String
filePath);
}
}使用很簡單,主要就是開始錄音startRecord()、取消錄音cancelRecord()、結(jié)束錄音stopRecord()和錄音監(jiān)聽setOnAudioStatusUpdateListener(),注意,取消錄音不保存文件,結(jié)束錄音會保存文件!在布局文件中添加一個(gè)控件(任意一個(gè)都行)<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按住說話"
android:textColor="@android:color/white"
android:id="@+id/button"
android:background="@color/colorPrimary"
/>在Activity中使用:
//當(dāng)前布局文件的根layout
final
RelativeLayout
rl
=
(RelativeLayout)
findViewById(R.id.rl);
mButton
=
(Button)
findViewById(R.id.button);
//PopupWindow的布局文件
final
View
view
=
View.inflate(this,
R.layout.layout_microphone,
null);
final
PopupWindowFactory
mPop
=
new
PopupWindowFactory(this,view);
//PopupWindow布局文件里面的控件
mImageView
=
(ImageView)
view.findViewById(R.id.iv_recording_icon);
mTextView
=
(TextView)
view.findViewById(R.id.tv_recording_time);
mAudioRecoderUtils
=
new
AudioRecoderUtils();
//錄音回調(diào)
mAudioRecoderUtils.setOnAudioStatusUpdateListener(new
AudioRecoderUtils.OnAudioStatusUpdateListener()
{
//錄音中db為聲音分貝,time為錄音時(shí)長
@Override
public
void
onUpdate(double
db,
long
time)
{
//根據(jù)分貝值來設(shè)置錄音時(shí)話筒圖標(biāo)的上下波動(dòng),下面有講解
mImageView.getDrawable().setLevel((int)
(3000
+
6000
*
db
/
100));
mTextView.setText(TimeUtils.long2String(time));
}
//錄音結(jié)束,filePath為保存路徑
@Override
public
void
onStop(String
filePath)
{
Toast.makeText(MainActivity.this,
"錄音保存在:"
+
filePath,
Toast.LENGTH_SHORT).show();
mTextView.setText(TimeUtils.long2String(0));
}
});
//Button的touch監(jiān)聽
mButton.setOnTouchListener
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 賣書快遞合同范本
- 廣州課題申報(bào)書怎么寫
- 雙方簽訂獨(dú)家合同范本
- 各種合同范本里
- 調(diào)查現(xiàn)狀課題申報(bào)書
- 幼兒校級課題申報(bào)書范文
- 創(chuàng)鑫供貨合同范本
- 名酒酒廠供貨合同范本
- 化妝 攝影 服務(wù)合同范本
- 教研課題申報(bào)書
- 深圳市一模+Z20名校聯(lián)盟+浙江名校協(xié)作體高三語文2月聯(lián)考作文題目解析及相關(guān)范文
- 油氣田開發(fā)專業(yè)危害因素辨識與風(fēng)險(xiǎn)防控
- 2025年浙江省寧波市外事服務(wù)中心招聘2人歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- 【MOOC】斷層影像解剖學(xué)-山東大學(xué) 中國大學(xué)慕課MOOC答案
- 2024CSCO結(jié)直腸癌診療指南解讀
- 近三年無行賄犯罪行為承諾書
- 新西蘭交通規(guī)則非常適用精編版
- Counting stars 歌詞38502
- STATA與面板數(shù)據(jù)回歸
- 燈具安裝驗(yàn)收表(精華版)
- 中國電信筆試題目大全(終極版)
評論
0/150
提交評論