Android 实现简单的录音功能

2017-09-13 10:43:11来源:网络收集作者:咖啡不加糖人点击

分享

MainActivity.class


package cn.bgs.luyin;
import java.io.IOException;
import Android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button mBtn1,mBtn2,mBtn3;
private RecodUtils utils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
utils=new RecodUtils();
initView();
}
private void initView() {
mBtn1=(Button) findViewById(R.id.mBtn1);
mBtn2=(Button) findViewById(R.id.mBtn2);
mBtn3=(Button) findViewById(R.id.mBtn3);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
mBtn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int ID=v.getId();
switch (ID) {
case R.id.mBtn1:
//开始录制
utils.startRecod(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abc.amr");
break;
case R.id.mBtn2:
utils.stopRecod();
break;
case R.id.mBtn3:
MediaPlayer player=new MediaPlayer();
try {
player.setDataSource(this, Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abc.amr"));
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}


RecodUtils.class


package cn.bgs.luyin;
import java.io.IOException;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioEncoder;
import android.media.MediaRecorder.AudioSource;
import android.media.MediaRecorder.OutputFormat;
/*
 * 媒体录制工具类
 * */
public class RecodUtils {
private String path;
private MediaRecorder recod;//Android中的媒体录制类//创建Recod类的方法
private void createRecod(){
recod=new MediaRecorder();
//给recod添加媒体源
recod.setAudioSource(AudioSource.MIC);
//给recod设置输出格式
recod.setOutputFormat(OutputFormat.AMR_NB);
recod.setAudioEncoder(AudioEncoder.AMR_NB);
recod.setOutputFile(path);
}
public void startRecod(String path){
this.path=path;
if(recod!=null){
return;
}
createRecod();//准备---》启动
try {
recod.prepare();
recod.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stopRecod(){
if(recod==null){
return;
}
recod.stop();
recod.release();
recod=null;
System.gc();
}
}

XML


activity_main.XML


    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    >
   android:id="@+id/mBtn1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="录制"
   />
   android:id="@+id/mBtn2"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="停止"
   />
   android:id="@+id/mBtn3"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="播放"
   />




最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台