Android錄音應用實例教程
本文以實例形式較為詳細的展示了Android錄音的實現(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>
運行效果如下圖所示:

MainActivity中定義按鈕的點擊監(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ù)當前時間給創(chuàng)建一個錄音文件,保存到預定的目錄中,用MediaRecorder類開始錄音。
// 開始錄音
public void start_record(){
if (!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
show_status("SD卡不存在,請插入SD卡!");
}
else{
try
{
// 獲取當前時間
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();
// 設置錄音的聲音來源
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 設置錄制的聲音的輸出格式(必須在設置聲音編碼格式之前設置)
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 設置聲音編碼的格式
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程序設計有所幫助。
- Android音頻錄制MediaRecorder之簡易的錄音軟件實現(xiàn)代碼
- Android簡單的利用MediaRecorder進行錄音的實例代碼
- Android應用開發(fā):電話監(jiān)聽和錄音代碼示例
- Android App調(diào)用MediaRecorder實現(xiàn)錄音功能的實例
- Android開發(fā)四大組件之實現(xiàn)電話攔截和電話錄音
- Android使用MediaRecorder實現(xiàn)錄音及播放
- 一個html5播放視頻的video控件只支持android的默認格式mp4和3gp
- 詳解Android開發(fā)之MP4文件轉(zhuǎn)GIF文件
- Android 使用VideoView播放MP4的簡單實現(xiàn)
- Android錄音并且輸出為Mp4文件的方法教程
相關文章
Android ListView position詳解及實例代碼
這篇文章主要介紹了Android ListView position的相關資料,在開發(fā)Android 應用的時候你真的用對了嗎?這里給大家徹底解釋下,需要的朋友可以參考下2016-10-10
Kotlin 使用高階函數(shù)實現(xiàn)回調(diào)方式
這篇文章主要介紹了Kotlin 使用高階函數(shù)實現(xiàn)回調(diào)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android getActivity()為空的問題解決辦法
這篇文章主要介紹了Android getActivity()為空的問題解決辦法的相關資料,導致apk空指針崩潰問題,很嚴重的問題,為了解決這問題,上網(wǎng)搜索了很多資料,需要的朋友可以參考下2017-07-07
Android View與Compose互相調(diào)用實例探究
這篇文章主要介紹了Android View與Compose互相調(diào)用,Compose 具有超強的兼容性,兼容現(xiàn)有的所有代碼,Compose 能夠與現(xiàn)有 View 體系并存,可實現(xiàn)漸進式替換2023-01-01

