标签:file prepare source turn comment uac ide esc 格式
android提供了MediaRecorder类,通过MediaRecorder录制音频的过程很简单,按步骤进行即可;在很多开发项目中,我们见到代码的封装很好;常常感觉这才是大牛写出的代码,其实我们也是可以写出来的,今天就通过一个MediaRecorder录制音频的实例,进行代码的抽取实现封装;
1:创建MediaRecorder对象;
2:调用MediaRecorder对象的setAudioSource()方法设置声音来源,一般传入MediaRecorder.AudioSource.MIC参数,指定录制来自麦克风的声音;
3:调用MediaRecorder对象的setOutputFormat()设置所录制的音频文件的格式;
4:调用MediaRecorder对象的setAudioEncoder()、setAudioEncodingBitRate(int bitRate)、setAudioSamplingRate(int samplingRate)设置所录制的声音的编码格式、编码位率、采样率等;这些参数将控制所录制音频的频率,文件的大小;一般来说,声音品质越好,文件越大;
5:调用MediaRedorder的setOutputFile(String path)方法,设置录制的音频文件的保存位置;
6:调用MediaRecorder的prepare()方法,准备录制;
7:调用MdiaRecorder的start()方法,开始录制;
8:录制完成,调用MediaRecorder对象的stop()方法停止录制,并调用release()方法释放资源;
注意:上面的第3 和第4 两个步骤,千万不能搞反;否则程序将会抛出 IllegalStateException 异常;
我们参考MediaRecorder的状态图就可以明白MediaRecorder录制音频的步骤:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/openRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Record"
android:textSize="20sp"
/>
<Button
android:id="@+id/closeRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Close Record"
android:textSize="20sp"
/>
<Button
android:id="@+id/playSound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play Sound"
android:textSize="20sp"
/>
<Button
android:id="@+id/stopSound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Sound"
android:textSize="20sp"
/>
</LinearLayout>
四个按钮作用分别是:开始录制音频、停止录制音频、开始播放音频、停止播放音频
public class MyActivity extends Activity implements OnClickListener{
private Button bt_start;
private Button bt_stop;
private Button bt_play;
private Button bt_stopSound;
private MediaRecorder mRecorder;
private MediaPlayer mPlayer;
private File soundFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
bt_start = (Button) this.findViewById(R.id.openRecord);
bt_stop = (Button) this.findViewById(R.id.closeRecord);
bt_play = (Button) this.findViewById(R.id.playSound);
bt_stopSound = (Button) this.findViewById(R.id.stopSound);
bt_start.setOnClickListener(this);
bt_stop.setOnClickListener(this);
bt_play.setOnClickListener(this);
bt_stopSound.setOnClickListener(this);
bt_play.setEnabled(false);
bt_stopSound.setEnabled(false);
mRecorder = new MediaRecorder();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.openRecord:
startRecord();
break;
case R.id.closeRecord:
stopRecord();
break;
case R.id.playSound:
playSound();
break;
case R.id.stopSound:
stopSound();
break;
default:
break;
}
}
private void stopSound() {
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
private void stopRecord() {
if(null != mRecorder){
mRecorder.stop();
mRecorder.release();
bt_play.setEnabled(true);
bt_stopSound.setEnabled(true);
}
}
private void playSound() {
if(soundFile != null && soundFile.exists()){
try {
mPlayer = new MediaPlayer();
mPlayer.reset();
mPlayer.setDataSource(soundFile.getAbsolutePath());
mPlayer.prepare();
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void startRecord() {
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//Toast.makeText(MainActivity.this, "SD卡不存在,请插入SD卡!", Toast.LENGTH_LONG).show();
return;
}
try {
//创建保存录制音频的文件
soundFile = new File(Environment.getExternalStorageDirectory().getCanonicalFile(),"/sound.amr");
mRecorder.reset();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(soundFile.getAbsolutePath());
mRecorder.prepare();
mRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
if(null != mRecorder){
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
if(mPlayer != null){
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
super.onDestroy();
}
}
在上面的MainActivity代码中,我们可以看到虽然我们对代码进行了很好的模块化,但感觉如果我们把MediaRecorder 重新封装,MainActivity的代码量会更简介;好了,现在我们就开始通过自定义一个MyMediaRecorder来封装MediaRecorder,在MainActivity中我们只需通过MyMediaRecorder的封装来调用MediaRecorder,可以简化MainActivity中的代码量;
public class MyMediaRecorder {
private MediaRecorder mRecorder;
private Context context;
public MyMediaRecorder(Context context){
mRecorder = new MediaRecorder();
this.context = context;
}
public void startRecord(File soundFile){
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(context, "SD卡不存在,请插入SD卡!", Toast.LENGTH_LONG).show();
return;
}
try {
mRecorder.reset();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(soundFile.getAbsolutePath());
mRecorder.prepare();
mRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopRecord(){
if(null != mRecorder){
mRecorder.stop();
mRecorder.release();
}
}
public void onDestroy(){
if(null != mRecorder){
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
}
同样我们也可以把MediaPlayer进行封装:
public class MyMediaPlayer {
private MediaPlayer mPlayer;
public MyMediaPlayer(){
mPlayer = new MediaPlayer();
}
public void playSound(File soundFile){
try {
mPlayer.reset();
mPlayer.setDataSource(soundFile.getAbsolutePath());
mPlayer.prepare();
mPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopSound(){
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
public void onDestroy(){
if(mPlayer != null){
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
}
public class MyActivity02 extends Activity implements OnClickListener{
private Button bt_start;
private Button bt_stop;
private Button bt_play;
private Button bt_stopSound;
private MyMediaRecorder myMediaRecorder;
private MyMediaPlayer myMediaPlayer;
private File soundFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
bt_start = (Button) this.findViewById(R.id.openRecord);
bt_stop = (Button) this.findViewById(R.id.closeRecord);
bt_play = (Button) this.findViewById(R.id.playSound);
bt_stopSound = (Button) this.findViewById(R.id.stopSound);
bt_start.setOnClickListener(this);
bt_stop.setOnClickListener(this);
bt_play.setOnClickListener(this);
bt_stopSound.setOnClickListener(this);
bt_play.setEnabled(false);
bt_stopSound.setEnabled(false);
//myMediaRecorder = new MyMediaRecorder(MainActivity.this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.openRecord:
startRecord();
break;
case R.id.closeRecord:
stopRecord();
break;
case R.id.playSound:
playSound();
break;
case R.id.stopSound:
stopSound();
break;
default:
break;
}
}
public void stopSound() {
myMediaPlayer.stopSound();
}
public void playSound() {
if(soundFile != null && soundFile.exists()){
myMediaPlayer = new MyMediaPlayer();
myMediaPlayer.playSound(soundFile);
}
}
public void stopRecord() {
myMediaRecorder.stopRecord();
bt_play.setEnabled(true);
bt_stopSound.setEnabled(true);
}
public void startRecord() {
try {
soundFile = new File(Environment.getExternalStorageDirectory().getCanonicalFile(),"/sound.amr");
if(soundFile != null && soundFile.exists()){
myMediaRecorder.startRecord(soundFile);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onDestroy() {
myMediaRecorder.onDestroy();
myMediaPlayer.onDestroy();
}
}
现在,我们在MainActivity中只需要通过封装好的MyMediaRecorder、MyMediaPlayer可以实现音频的录制;复杂的代码都被我们封装起来了,所以在我们的MainActivity中,代码看上去更精炼;
现在我们看一下我们的MainActivity 还能更进一步封装吗?
其实代码的封装就是把有共性的给抽取出来,放到另一个类中;现在我们可以看到在我们的MainActivity中,各种视图控件看上去占据了大部分代码量,很繁琐;由于视图控件是一个界面,主要是与用户进行交互的;看到他们的共性点,所以我们也是可以进行抽取的;
我把所有在MainActivity中出现的视图控件,去不都抽取到MyManager中:
public class MyManager implements OnClickListener{
private Button bt_start;
private Button bt_stop;
private Button bt_play;
private Button bt_stopSound;
private MyMediaRecorder myMediaRecorder;
private MyMediaPlayer myMediaPlayer;
private File soundFile;
private MainActivity context;
public MyManager(Context context){
this.context = (MainActivity) context;
}
public void initView(){
bt_start = (Button) context.findViewById(R.id.openRecord);
bt_stop = (Button) context.findViewById(R.id.closeRecord);
bt_play = (Button) context.findViewById(R.id.playSound);
bt_stopSound = (Button) context.findViewById(R.id.stopSound);
bt_start.setOnClickListener(this);
bt_stop.setOnClickListener(this);
bt_play.setOnClickListener(this);
bt_stopSound.setOnClickListener(this);
bt_play.setEnabled(false);
bt_stopSound.setEnabled(false);
myMediaRecorder = new MyMediaRecorder(context);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.openRecord:
startRecord();
break;
case R.id.closeRecord:
stopRecord();
break;
case R.id.playSound:
playSound();
break;
case R.id.stopSound:
stopSound();
break;
default:
break;
}
}
public void stopSound() {
myMediaPlayer.stopSound();
}
public void playSound() {
if(soundFile != null && soundFile.exists()){
mhuachengj1980.comyMediaPlayer = new MyMediaPlayer();
myMediaPlayer.playSound(soundFile);
}
}
public vo078881.cnid stopRecord() {
myMediaRecorder.stopRecord();
bt_play.setEnabled(true);
bt_sto2636666.cnpSound.setEnabled(true);
}
public void startRecord() {
try {
soundFile = new File(Environment.getExternalStorageDirectory().getCanonicalFile(),"/sound.amr");
if(soundFile != null && soundFile.exists()){
myMediaRecorder.startRecord(soundFile);
}
} catch (IOException e) {
//mhylpt.com TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onDestroy() {
myMediaRecorder.onDestroy();
myMediaPlayer.onDestroy();
}
}
现在让我们再看一下MainActivity 的代码:
public class MainActivity extends Activity {
private MyManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
manager = new MyManager(MainActivity.this);
manager.initView();
}
@Override
protected void onDestroy() {
manager.onDestroy();
}
}
是不是很惊讶,简简单单的4行代码;对!!!这就是抽取、封装的重要性,它能把我们的代码进行提炼,让代码更精炼,更方便阅读;
标签:file prepare source turn comment uac ide esc 格式
原文地址:https://www.cnblogs.com/eryaketang/p/8962435.html