Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼
使用MediaRecorder的步驟:
1、創(chuàng)建MediaRecorder對(duì)象
2、調(diào)用MediRecorder對(duì)象的setAudioSource()方法設(shè)置聲音的來(lái)源,一般傳入MediaRecorder.MIC
3、調(diào)用MediaRecorder對(duì)象的setOutputFormat()設(shè)置所錄制的音頻文件的格式
4、調(diào)用MediaRecorder對(duì)象的setAudioRncoder()、setAudioEncodingBitRate(int bitRate)、setAudioSamlingRate(int SamplingRate)設(shè)置所錄音的編碼格式、編碼位率、采樣率等,
5、調(diào)用MediaRecorder對(duì)象的setOutputFile(String path)方法設(shè)置錄制的音頻文件的保存位置
6、調(diào)用MediaRecoder對(duì)象的Prepare()方法準(zhǔn)備錄制
7、調(diào)用MediaRecoder對(duì)象的start()方法開(kāi)始錄制
8、調(diào)用MediaRecoder對(duì)象的stop()方法停止錄制,并調(diào)用release()方法釋放資源
實(shí)例:
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/li1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/start"/>
<Button android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/stop"/>
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_below="@id/li1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show_file_name" />
<Button
android:id="@+id/bt_list_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/play"/>
<Button android:id="@+id/bt_list_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_stop"/>
</LinearLayout>
package com.android.xiong.mediarecordertest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button start;
private Button stop;
private ListView listView;
// 錄音文件播放
private MediaPlayer myPlayer;
// 錄音
private MediaRecorder myRecorder;
// 音頻文件保存地址
private String path;
private String paths = path;
private File saveFilePath;
// 所錄音的文件
String[] listFile = null;
ShowRecorderAdpter showRecord;
AlertDialog aler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
listView = (ListView) findViewById(R.id.list);
myPlayer = new MediaPlayer();
myRecorder = new MediaRecorder();
// 從麥克風(fēng)源進(jìn)行錄音
myRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
// 設(shè)置輸出格式
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
// 設(shè)置編碼格式
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
showRecord = new ShowRecorderAdpter();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
try {
path = Environment.getExternalStorageDirectory()
.getCanonicalPath().toString()
+ "/XIONGRECORDERS";
File files = new File(path);
if (!files.exists()) {
files.mkdir();
}
listFile = files.list();
} catch (IOException e) {
e.printStackTrace();
}
}
start.setOnClickListener(this);
stop.setOnClickListener(this);
if (listFile != null) {
listView.setAdapter(showRecord);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ShowRecorderAdpter extends BaseAdapter {
@Override
public int getCount() {
return listFile.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(final int postion, View arg1, ViewGroup arg2) {
View views = LayoutInflater.from(MainActivity.this).inflate(
R.layout.list_show_filerecorder, null);
TextView filename = (TextView) views
.findViewById(R.id.show_file_name);
Button plays = (Button) views.findViewById(R.id.bt_list_play);
Button stop = (Button) views.findViewById(R.id.bt_list_stop);
filename.setText(listFile[postion]);
// 播放錄音
plays.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
myPlayer.reset();
myPlayer.setDataSource(path + "/" + listFile[postion]);
if (!myPlayer.isPlaying()) {
myPlayer.prepare();
myPlayer.start();
} else {
myPlayer.pause();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
// 停止播放
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (myPlayer.isPlaying()) {
myPlayer.stop();
}
}
});
return views;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
final EditText filename = new EditText(this);
Builder alerBuidler = new Builder(this);
alerBuidler
.setTitle("請(qǐng)輸入要保存的文件名")
.setView(filename)
.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
String text = filename.getText().toString();
try {
paths = path
+ "/"
+ text
+ new SimpleDateFormat(
"yyyyMMddHHmmss").format(System
.currentTimeMillis())
+ ".amr";
saveFilePath = new File(paths);
myRecorder.setOutputFile(saveFilePath
.getAbsolutePath());
saveFilePath.createNewFile();
myRecorder.prepare();
// 開(kāi)始錄音
myRecorder.start();
start.setText("正在錄音中。。");
start.setEnabled(false);
aler.dismiss();
// 重新讀取 文件
File files = new File(path);
listFile = files.list();
// 刷新ListView
showRecord.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
});
aler = alerBuidler.create();
aler.setCanceledOnTouchOutside(false);
aler.show();
break;
case R.id.stop:
if (saveFilePath.exists() && saveFilePath != null) {
myRecorder.stop();
myRecorder.release();
// 判斷是否保存 如果不保存則刪除
new AlertDialog.Builder(this)
.setTitle("是否保存該錄音")
.setPositiveButton("確定", null)
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
saveFilePath.delete();
// 重新讀取 文件
File files = new File(path);
listFile = files.list();
// 刷新ListView
showRecord.notifyDataSetChanged();
}
}).show();
}
start.setText("錄音");
start.setEnabled(true);
default:
break;
}
}
@Override
protected void onDestroy() {
// 釋放資源
if (myPlayer.isPlaying()) {
myPlayer.stop();
myPlayer.release();
}
myPlayer.release();
myRecorder.release();
super.onDestroy();
}
}
源碼下載:http://xiazai.jb51.net/201401/yuanma/MediaRecorderTest(jb51.net).rar
- Android給通知channel靜音的方法實(shí)例
- Android實(shí)現(xiàn)靜音檢測(cè)功能
- Android 判斷網(wǎng)絡(luò)狀態(tài)對(duì)音頻靜音的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)定時(shí)自動(dòng)靜音小助手
- Android EasyPlayer聲音自動(dòng)停止、恢復(fù),一鍵靜音等功能
- android實(shí)現(xiàn)來(lái)電靜音示例(監(jiān)聽(tīng)來(lái)電)
- android系統(tǒng)在靜音模式下關(guān)閉camera拍照聲音的方法
- Android簡(jiǎn)單的利用MediaRecorder進(jìn)行錄音的實(shí)例代碼
- Android實(shí)現(xiàn)錄音功能實(shí)現(xiàn)實(shí)例(MediaRecorder)
- Android實(shí)現(xiàn)錄音靜音降噪
相關(guān)文章
Android自定義實(shí)現(xiàn)BaseAdapter的優(yōu)化布局
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)BaseAdapter的優(yōu)化布局,感興趣的小伙伴們可以參考一下2016-08-08Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果,幫助大家制作網(wǎng)易客戶端導(dǎo)航欄特效,感興趣的小伙伴們可以參考一下2016-06-06Android仿貼吧內(nèi)容下的簡(jiǎn)單ListView嵌套GridView
這篇文章主要為大家詳細(xì)介紹了Android仿貼吧內(nèi)容下的簡(jiǎn)單ListView嵌套GridView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android 媒體開(kāi)發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析
這篇文章主要介紹了Android 媒體開(kāi)發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析,需要的朋友可以參考下2017-08-08Android開(kāi)發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法,結(jié)合實(shí)例形式分析了Android針對(duì)assets目錄下SQLite數(shù)據(jù)庫(kù)文件的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android實(shí)現(xiàn)斷點(diǎn)多線程下載
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)斷點(diǎn)多線程下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼
大家好,本篇文章主要講的是Android實(shí)現(xiàn)流光和光影移動(dòng)效果代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android編程實(shí)現(xiàn)3D滑動(dòng)旋轉(zhuǎn)效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)3D滑動(dòng)旋轉(zhuǎn)效果的方法,主要通過(guò)繼承Animation自定義Rotate3D來(lái)實(shí)現(xiàn)3D翻頁(yè)效果,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11