Android(安卓)简单录音程序
16lz
2022-04-22
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文件夹大小
- android 字体大小,样式 ,像素的设置
- Android(安卓)Design Support Library(一):FloatingActionButton、T
- 第一章:初入Android大门(弹出对话框)
- Android(安卓)---android:autoLink属性
- android openGL 预览camera/camear2
- NPM 和webpack 的基础使用
随机推荐
- BRCM5.02编译四: ERROR: lzo/lzo1x.h deve
- 【故障处理】队列等待之enq: US - conten
- 【故障处理】DG环境主库丢失归档情况下数
- BRCM5.02编译二:Error: Could not retreiv
- 【故障处理】队列等待之TX - allocate IT
- 【等待事件】System I/O类 等待事件(3.2)--
- BRCM5.02编译五: fatal error: uuid/uuid.
- 【等待事件】User I/O类 等待事件(2.7)--di
- BRCM5.02编译一 : 缺少工具链路
- Flask 入门一( flask 框架和 flask-script