這期內容當中小編將會給大家帶來有關在Android應用中實現一個錄音播放功能,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
播放音頻文件
/**
* Created by zhb on 2017/1/16.
* 音樂在線播放
*/
public class PlayManager {
private Context mcontext;
public PlayManager(Context context){
this.mcontext = context;
}
public void play(String song){
MediaPlayer mp = new MediaPlayer();
try {
// 存儲在SD卡或其他文件路徑下的媒體文件
// 例如:mp.setDataSource("/sdcard/test.mp3");
// 網絡上的媒體文件
// 例如:mp.setDataSource("http://www...../music/test.mp3");
mp.setDataSource(song);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}2.錄制amr格式音頻文件(微信語音便用的這種格式,至于音頻文件格式之間的比較請自行百度)
/**
* Created by zhb on 2017/1/16.
* 本地錄音
*/
public class RecordManager {
//錄制成amr格式............................................................
private Context mcontext;
MediaRecorder mediaRecorder ;
public RecordManager(Context context){
this.mcontext = context;
//TODO 初始化安裝路徑,錄音流程
}
/**開始錄制*/
public void start_amr(){
mediaRecorder = new MediaRecorder();
/**
* mediaRecorder.setAudioSource設置聲音來源。
* MediaRecorder.AudioSource這個內部類詳細的介紹了聲音來源。
* 該類中有許多音頻來源,不過最主要使用的還是手機上的麥克風,MediaRecorder.AudioSource.MIC
*/
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
/**
* mediaRecorder.setOutputFormat代表輸出文件的格式。該語句必須在setAudioSource之后,在prepare之前。
* OutputFormat內部類,定義了音頻輸出的格式,主要包含MPEG_4、THREE_GPP、RAW_AMR……等。
*/
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
/**
* mediaRecorder.setAddioEncoder()方法可以設置音頻的編碼
* AudioEncoder內部類詳細定義了兩種編碼:AudioEncoder.DEFAULT、AudioEncoder.AMR_NB
*/
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
/**
* 設置錄音之后,保存音頻文件的位置,一般是SD卡的位置
*/
mediaRecorder.setOutputFile(String.valueOf(PathManger.getVoicePath()));
/**
* 調用start開始錄音之前,一定要調用prepare方法。
*/
try {
mediaRecorder.prepare();
mediaRecorder.start();
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
/**停止錄音*/
public void stop_amr(){
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
/**重置錄音*/
public void reset_amr(){
mediaRecorder.reset();
}
}3、配置轉換工具包(這個比較簡單,配置以下文件即可)
添加flame.jar,并在armeabi和armeabi-v7a文件夾添加libmp3lame.so
資源文件:http://xiazai.jb51.net/201701/yuanma/androidlibmp3lame(jb51.net).rar
4、錄制MP3格式音頻文件(個人覺得這種格式能比較好的統一Android端和iOS端的音頻文件,雖然方法相對比較繁雜一些)
/**
* Created by zhb on 2017/1/16.
* 本地錄音
*/
public class RecordManager {
//錄制成MP3格式..............................................
/**構造時候需要的Activity,主要用于獲取文件夾的路徑*/
private Activity activity;
/**文件代號*/
public static final int RAW = 0X00000001;
public static final int MP3 = 0X00000002;
/**文件路徑*/
private String rawPath = null;
private String mp3Path = null;
/**采樣頻率*/
private static final int SAMPLE_RATE = 11025;
/**錄音需要的一些變量*/
private short[] mBuffer;
private AudioRecord mRecorder;
/**錄音狀態*/
private boolean isRecording = false;
/**是否轉換ok*/
private boolean convertOk = false;
public RecordManager(Activity activity, String rawPath, String mp3Path) {
this.activity = activity;
this.rawPath = rawPath;
this.mp3Path = mp3Path;
}
/**開始錄音*/
public boolean start_mp3() {
// 如果正在錄音,則返回
if (isRecording) {
return isRecording;
}
// 初始化
if (mRecorder == null) {
initRecorder();
}
getFilePath();
mRecorder.startRecording();
startBufferedWrite(new File(rawPath));
isRecording = true;
return isRecording;
}
/**停止錄音,并且轉換文件,這很可能是個耗時操作,建議在后臺中做*/
public boolean stop_mp3() {
if (!isRecording) {
return isRecording;
}
// 停止
mRecorder.stop();
isRecording = false;
//TODO
// 開始轉換
FLameUtils lameUtils = new FLameUtils(1, SAMPLE_RATE, 96);
convertOk = lameUtils.raw2mp3(rawPath, mp3Path);
return isRecording ^ convertOk;// convertOk==true,return true
}
/**獲取文件的路徑*/
public String getFilePath(int fileAlias) {
if (fileAlias == RAW) {
return rawPath;
} else if (fileAlias == MP3) {
return mp3Path;
} else
return null;
}
/**清理文件*/
public void cleanFile(int cleanFlag) {
File f = null;
try {
switch (cleanFlag) {
case MP3:
f = new File(mp3Path);
if (f.exists())
f.delete();
break;
case RAW:
f = new File(rawPath);
if (f.exists())
f.delete();
break;
case RAW | MP3:
f = new File(rawPath);
if (f.exists())
f.delete();
f = new File(mp3Path);
if (f.exists())
f.delete();
break;
}
f = null;
} catch (Exception e) {
e.printStackTrace();
}
}
/**關閉,可以先調用cleanFile來清理文件*/
public void close() {
if (mRecorder != null)
mRecorder.release();
activity = null;
}
/**初始化*/
private void initRecorder() {
int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
mBuffer = new short[bufferSize];
mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
}
/**設置路徑,第一個為raw文件,第二個為mp3文件*/
private void getFilePath() {
try {
String folder = "audio_recorder_2_mp3";
String fileName = String.valueOf(System.currentTimeMillis());
if (rawPath == null) {
File raw = new File(activity.getDir(folder,
activity.MODE_PRIVATE), fileName + ".raw");
raw.createNewFile();
rawPath = raw.getAbsolutePath();
raw = null;
}
if (mp3Path == null) {
File mp3 = new File(activity.getDir(folder,
activity.MODE_PRIVATE), fileName + ".mp3");
mp3.createNewFile();
mp3Path = mp3.getAbsolutePath();
mp3 = null;
}
Log.d("rawPath", rawPath);
Log.d("mp3Path", mp3Path);
runCommand("chmod 777 " + rawPath);
runCommand("chmod 777 " + mp3Path);
} catch (Exception e) {
e.printStackTrace();
}
}
/**執行cmd命令,并等待結果*/
private boolean runCommand(String command) {
boolean ret = false;
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
ret = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
return ret;
}
/**寫入到raw文件*/
private void startBufferedWrite(final File file) {
new Thread(new Runnable() {
@Override
public void run() {
DataOutputStream output = null;
try {
output = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(file)));
while (isRecording) {
int readSize = mRecorder.read(mBuffer, 0,
mBuffer.length);
for (int i = 0; i < readSize; i++) {
output.writeShort(mBuffer[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}).start();
}
}5、最后在自己想調用的地方調用就好了,PathManger這個是我自己的路徑管理工具,這里不貼了,反正自己直接放一個路徑字符串進去就好了
/**初始化語音*/
private void initVoice() {
//錄音
RecordManager = new RecordManager(
CallHelpActivity.this,
String.valueOf(PathManger.getVoicePathToRaw()),
String.valueOf(PathManger.getVoicePathToMp3()));
callHelp_Voice_longclick.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
RecordManager.start_mp3();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
RecordManager.stop_mp3();
break;
}
return false;
}
});
//語音播放
final PlayManager PlayManager = new PlayManager(this);
callHelp_Voice_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PlayManager.play(String.valueOf(PathManger.getVoicePathToMp3()));
}
});
}上述就是小編為大家分享的在Android應用中實現一個錄音播放功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。