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)聽器,按下按鈕時(shí)開始錄音,松開按鈕時(shí)停止錄音,類似于微信的操作方法。
// 獲得控件
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)前時(shí)間給創(chuàng)建一個(gè)錄音文件,保存到預(yù)定的目錄中,用MediaRecorder類開始錄音。
// 開始錄音
public void start_record(){
if (!Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
show_status("SD卡不存在,請(qǐng)插入SD卡!");
}
else{
try
{
// 獲取當(dāng)前時(shí)間
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());
}
}
}
停止錄音的方法,相對(duì)簡單。
// 停止錄音
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文件可以正常播放。
希望本文所述對(duì)大家的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)錄音及播放
- 一個(gè)html5播放視頻的video控件只支持android的默認(rèn)格式mp4和3gp
- 詳解Android開發(fā)之MP4文件轉(zhuǎn)GIF文件
- Android 使用VideoView播放MP4的簡單實(shí)現(xiàn)
- Android錄音并且輸出為Mp4文件的方法教程
相關(guān)文章
Android ListView position詳解及實(shí)例代碼
這篇文章主要介紹了Android ListView position的相關(guān)資料,在開發(fā)Android 應(yīng)用的時(shí)候你真的用對(duì)了嗎?這里給大家徹底解釋下,需要的朋友可以參考下2016-10-10
Android Service開發(fā)應(yīng)用實(shí)例
Android的服務(wù)是開發(fā)Android應(yīng)用程序的重要組成部分。不同于活動(dòng)Activity,服務(wù)是在后臺(tái)運(yùn)行,服務(wù)沒有接口,生命周期也與活動(dòng)Activity非常不同。通過使用服務(wù)我們可以實(shí)現(xiàn)一些后臺(tái)操作,比如想從遠(yuǎn)程服務(wù)器加載一個(gè)網(wǎng)頁等,下面來看看詳細(xì)內(nèi)容,需要的朋友可以參考下2022-12-12
Android三種實(shí)現(xiàn)定時(shí)器的方法
本文給大家分享了3種Android實(shí)現(xiàn)定時(shí)器的方法的示例,,需要的朋友可以參考下2015-02-02
Kotlin 使用高階函數(shù)實(shí)現(xiàn)回調(diào)方式
這篇文章主要介紹了Kotlin 使用高階函數(shù)實(shí)現(xiàn)回調(diào)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android getActivity()為空的問題解決辦法
這篇文章主要介紹了Android getActivity()為空的問題解決辦法的相關(guān)資料,導(dǎo)致apk空指針崩潰問題,很嚴(yán)重的問題,為了解決這問題,上網(wǎng)搜索了很多資料,需要的朋友可以參考下2017-07-07
Android View與Compose互相調(diào)用實(shí)例探究
這篇文章主要介紹了Android View與Compose互相調(diào)用,Compose 具有超強(qiáng)的兼容性,兼容現(xiàn)有的所有代碼,Compose 能夠與現(xiàn)有 View 體系并存,可實(shí)現(xiàn)漸進(jìn)式替換2023-01-01

