Android編程錄音工具類RecorderUtil定義與用法示例
本文實(shí)例講述了Android編程錄音工具類RecorderUtil定義與用法。分享給大家供大家參考,具體如下:
以下工具類都是經(jīng)過實(shí)戰(zhàn)開發(fā)驗(yàn)證都是可以直接復(fù)制使用的。
錄音工具類介紹:
錄音工具類主要平時(shí)用來開發(fā)語音聊天的,在微信和QQ上該工具類都是常用的,因?yàn)檎Z音聊天。
使用硬件一般都要開權(quán)限,別忘了。這里還需要搭配 Android FileUtil 類使用,為了方便才這么封裝的
import android.media.MediaRecorder;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* 錄音工具
*/
public class RecorderUtil {
private static final String TAG = "RecorderUtil";
private String mFileName = null;
private MediaRecorder mRecorder = null;
private long startTime;
private long timeInterval;
private boolean isRecording;
public RecorderUtil(){
mFileName = FileUtil.getCacheFilePath("tempAudio");
}
/**
* 開始錄音
*/
public void startRecording() {
if (mFileName == null) return;
if (isRecording){
mRecorder.release();
mRecorder = null;
}
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
startTime = System.currentTimeMillis();
try {
mRecorder.prepare();
mRecorder.start();
isRecording = true;
} catch (Exception e){
Log.e(TAG, "prepare() failed");
}
}
/**
* 停止錄音
*/
public void stopRecording() {
if (mFileName == null) return;
timeInterval = System.currentTimeMillis() - startTime;
try{
if (timeInterval>1000){
mRecorder.stop();
}
mRecorder.release();
mRecorder = null;
isRecording =false;
}catch (Exception e){
Log.e(TAG, "release() failed");
}
}
/**
* 取消語音
*/
public synchronized void cancelRecording() {
if (mRecorder != null) {
try {
mRecorder.release();
mRecorder = null;
} catch (Exception e) {
e.printStackTrace();
}
File file = new File(mFileName);
file.deleteOnExit();
}
isRecording =false;
}
/**
* 獲取錄音文件
*/
public byte[] getDate() {
if (mFileName == null) return null;
try{
return readFile(new File(mFileName));
}catch (IOException e){
Log.e(TAG, "read file error" + e);
return null;
}
}
/**
* 獲取錄音文件地址
*/
public String getFilePath(){
return mFileName;
}
/**
* 獲取錄音時(shí)長,單位秒
*/
public long getTimeInterval() {
return timeInterval/1000;
}
/**
* 將文件轉(zhuǎn)化為byte[]
*
* @param file 輸入文件
*/
private static byte[] readFile(File file) throws IOException {
// Open file
RandomAccessFile f = new RandomAccessFile(file, "r");
try {
// Get and check length
long longlength = f.length();
int length = (int) longlength;
if (length != longlength)
throw new IOException("File size >= 2 GB");
// Read file and return data
byte[] data = new byte[length];
f.readFully(data);
return data;
} finally {
f.close();
}
}
}
使用步驟:
1. 首先private RecorderUtil recorder = new RecorderUtil(); 實(shí)例化一下
2. 開始錄音recorder.startRecording();
3. 錄音完成后停止錄音recorder.stopRecording();
4. 當(dāng)然如果錄音開始之后想取消語音發(fā)送,類似于微信上滑取消語音發(fā)送,解決方案滑動(dòng)監(jiān)聽判斷確定取消發(fā)送,就不要將消息發(fā)出去并且還要調(diào)用recorder.cancelRecording(); //取消語音釋放資源 即可
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能
- Android實(shí)現(xiàn)錄音功能實(shí)現(xiàn)實(shí)例(MediaRecorder)
- Android使用AudioRecord實(shí)現(xiàn)暫停錄音功能實(shí)例代碼
- Android錄音--AudioRecord、MediaRecorder的使用
- android 通過MediaRecorder實(shí)現(xiàn)簡(jiǎn)單的錄音示例
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- Android App調(diào)用MediaRecorder實(shí)現(xiàn)錄音功能的實(shí)例
- Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼
- Android簡(jiǎn)單的利用MediaRecorder進(jìn)行錄音的實(shí)例代碼
- Android使用AudioRecord實(shí)現(xiàn)錄音功能
相關(guān)文章
Android中使用Handler及Countdowntimer實(shí)現(xiàn)包含倒計(jì)時(shí)的閃屏頁面
這篇文章主要介紹了Android中使用Handler及Countdowntimer實(shí)現(xiàn)包含倒計(jì)時(shí)的閃屏頁面,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android之PreferenceActivity應(yīng)用詳解(2)
看到很多書中都沒有對(duì)PreferenceActivity做介紹,而我正好又在項(xiàng)目中用到,所以就把自己的使用的在這總結(jié)一下,也方便日后查找2012-11-11
Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法,實(shí)例分析了Android操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Android編程解析XML文件的方法詳解【基于XmlPullParser】
這篇文章主要介紹了Android編程解析XML文件的方法,結(jié)合實(shí)例形式分析了Android基于XmlPullParser解析xml文件的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-07-07
Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)
這篇文章主要介紹了Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)的相關(guān)資料,數(shù)據(jù)存儲(chǔ)是Android開發(fā)的重要的知識(shí),這里提供了實(shí)例,需要的朋友可以參考下2017-07-07
Android自定義View實(shí)現(xiàn)繪制虛線的方法詳解
這篇文章主要給大家介紹了Android自定義View實(shí)現(xiàn)繪制虛線的方法,在繪制過程中走了一些彎路才實(shí)現(xiàn)了虛線的效果,所以想著總結(jié)分享出來,方便有需要的朋友和自己在需要的時(shí)候參考學(xué)習(xí),下面來一起看看吧。2017-04-04
Android自定義view實(shí)現(xiàn)半圓環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)半圓環(huán)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android中使用achartengine生成圖表的具體方法
這篇文章主要介紹了Android中使用achartengine生成圖表的具體方法,有需要的朋友可以參考一下2014-01-01
Android中阻止AlertDialog關(guān)閉實(shí)例代碼
這篇文章主要介紹了Android阻止AlertDialog關(guān)閉實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-03-03

