Android 简单录音程序
16lz
2021-01-23
1.界面布局
2.添加权限及注册Serivce
3.主程序编写 public class MainActivity extends Activity {private Button startBT;private Button stopBT;private Button playBT;private Intent service;private boolean isRecording;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);startBT = (Button) findViewById(R.id.button1);stopBT = (Button) findViewById(R.id.button2);playBT = (Button) findViewById(R.id.button3);service = new Intent(this,MyService.class);isRecording = false;}public void start(View v){isRecording = true;setBtEnable(isRecording);startService(service);}public void stop(View v){isRecording = false;setBtEnable(isRecording);stopService(service);}private void setBtEnable(boolean flag) {startBT.setEnabled(!flag);stopBT.setEnabled(flag);playBT.setEnabled(!flag);}public void play(View v){Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(Uri.parse("file:///mnt/sdcard/records.3gp"),"audio/*");startActivity(intent);}public void delete(View v){File file = new File("/mnt/sdcard/records.3gp");try {if (file.exists()) {if (file.delete()) {Toast.makeText(getApplicationContext(), "删除成功", 0).show();}} else {Toast.makeText(getApplicationContext(), "文件不存在", 0).show();}} catch (Exception e) {// TODO: handle exception}}@Overrideprotected void onSaveInstanceState(Bundle outState) {// TODO Auto-generated method stuboutState.putBoolean("isRecording", isRecording);super.onSaveInstanceState(outState);}@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {// TODO Auto-generated method stubisRecording = savedInstanceState.getBoolean("isRecording");setBtEnable(isRecording);super.onRestoreInstanceState(savedInstanceState);}}
4.录音服务Service编写
public class MyService extends Service {private MediaRecorder recorder;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubrecorder = new MediaRecorder();recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置音频源 麦克风recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 设置输出格式 3GPrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置编码recorder.setOutputFile("/mnt/sdcard/records.3gp");//设置文件路径try {recorder.prepare();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//准备recorder.start();//开始super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubreturn super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubrecorder.stop();//停止录音recorder.release();//释放recorder = null;super.onDestroy();}}
更多相关文章
- AndroidManifest.xml文件详解(activity)(一)
- Android 实现windows文件资源管理器
- android 根据Uri获取文件绝对路径
- 重命名sd卡中的文件名
- android 使图片显示 圆角
- android用intent打开各种文件
- 调试 android jni 程序
- android ImageUtils 图片处理工具类