Android錄音應(yīng)用實(shí)例教程
本文以實(shí)例形式較為詳細(xì)的展示了Android錄音的實(shí)現(xiàn)方法,分享給大家供大家參考之用。具體方法如下:
首先是xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_talk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:enabled="false"
android:text="TALK"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
運(yùn)行效果如下圖所示:

MainActivity中定義按鈕的點(diǎn)擊監(jiān)聽器,按下按鈕時開始錄音,松開按鈕時停止錄音,類似于微信的操作方法。
// 獲得控件
public void get_con(){
btn_talk = (Button)findViewById(R.id.btn_talk);
btn_talk.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN){
// 開始錄音
start_record();
}
else if (e.getAction() == MotionEvent.ACTION_UP){
// 停止錄音
stop_record();
}
return false;
}
});
}
開始錄音的方法,使用了android.media.MediaRecorder錄音。首先判斷SD卡是否存在,如果存在根據(jù)當(dāng)前時間給創(chuàng)建一個錄音文件,保存到預(yù)定的目錄中,用MediaRecorder類開始錄音。
// 開始錄音
public void start_record(){
if (!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
show_status("SD卡不存在,請插入SD卡!");
}
else{
try
{
// 獲取當(dāng)前時間
cur_date = new Date(System.currentTimeMillis());
str_file = formatter.format(cur_date);
// 創(chuàng)建保存錄音的音頻文件
send_sound_file = new File(Environment.getExternalStorageDirectory().getCanonicalFile() + "/talk/send");
// 如果目錄不存在
if (!send_sound_file.exists()){
send_sound_file.mkdirs();
}
send_sound_file = new File(Environment.getExternalStorageDirectory().getCanonicalFile() + "/talk/send/" + str_file + ".amr");
recorder = new MediaRecorder();
// 設(shè)置錄音的聲音來源
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 設(shè)置錄制的聲音的輸出格式(必須在設(shè)置聲音編碼格式之前設(shè)置)
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 設(shè)置聲音編碼的格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(send_sound_file.getAbsolutePath());
recorder.prepare();
// 開始錄音
recorder.start();
}
catch (Exception e)
{
show_status(e.toString());
}
}
}
停止錄音的方法,相對簡單。
// 停止錄音
public void stop_record(){
if (send_sound_file != null && send_sound_file.exists())
{
ses_id = ses_id + 1;
// 停止錄音
recorder.stop();
// 釋放資源
recorder.release();
recorder = null;
}
super.onDestroy();
}
經(jīng)過測試,錄制的3gp文件可以正常播放。
希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
- Android音頻錄制MediaRecorder之簡易的錄音軟件實(shí)現(xiàn)代碼
- Android簡單的利用MediaRecorder進(jìn)行錄音的實(shí)例代碼
- Android應(yīng)用開發(fā):電話監(jiān)聽和錄音代碼示例
- Android App調(diào)用MediaRecorder實(shí)現(xiàn)錄音功能的實(shí)例
- Android開發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- 一個html5播放視頻的video控件只支持android的默認(rèn)格式mp4和3gp
- 詳解Android開發(fā)之MP4文件轉(zhuǎn)GIF文件
- Android 使用VideoView播放MP4的簡單實(shí)現(xiàn)
- Android錄音并且輸出為Mp4文件的方法教程
相關(guān)文章
Android實(shí)現(xiàn)3D標(biāo)簽云效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D標(biāo)簽云效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Kotlin語言中CompileSdkVersion與targetSdkVersion的區(qū)別淺析
這篇文章主要介紹了Kotlin語言中CompileSdkVersion和targetSdkVersion有什么區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
android?sharedUserId?使用知識盲點(diǎn)解析
這篇文章主要為大家介紹了android?sharedUserId使用的知識盲點(diǎn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Kotlin學(xué)習(xí)筆記之const val與val
這篇文章主要給大家介紹了關(guān)于Kotlin學(xué)習(xí)筆記之const val與val的相關(guān)資料,并給大家介紹了const val和val區(qū)別以及Kotlin中var和val的區(qū)別,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Android Studio自定義萬能注釋模板與創(chuàng)建類,方法注釋模板操作
這篇文章主要介紹了Android Studio自定義萬能注釋模板與創(chuàng)建類,方法注釋模板操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

