Android 录音组件MediaRecorder的使用 提供完整源码
2012-07-31 21:34
513 查看
Android 录音组件MediaRecorder的使用 提供完整源码
MediaRecorder可以很方便的拍照、录像、录音,本文只介绍录音的使用方法。虽然录音时声源可以选择通话,但是在真机上运行不行,不知如何解决,我机上(i897 lidroid 2.2 Rom)的录音通话软件很好用,所以说理论上还有有方法实现的,毕竟已经有人实现了。下面上代码:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始录音" /> <Button android:id="@+id/stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止" /> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1718 | package com.pocketdigi; import android.app.Activity; import android.media.MediaRecorder; import android.media.MediaRecorder.AudioSource; import android.media.MediaRecorder.OutputFormat; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MediaRecorderActivity extends Activity { Button start,stop; MediaRecorder mr; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start=(Button)findViewById(R.id.start); stop=(Button)findViewById(R.id.stop); start.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub mr=new MediaRecorder(); mr.setAudioSource(AudioSource.MIC); //设置音源,这里是来自麦克风,虽然有VOICE_CALL,但经真机测试,不行 mr.setOutputFormat(OutputFormat.RAW_AMR); //输出格式 mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //编码 mr.setOutputFile("/sdcard/1.amr"); //输出文件路径,貌似文件必须是不存在的,不会自己清空 try{ mr.prepare(); //做些准备工作 mr.start(); //开始 }catch(Exception e){ e.printStackTrace(); } }}); stop.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub mr.stop(); //停止 mr.release(); //释放 }}); } } |
1 2 | <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> |
源码打包:
MediaRecorder (330)
相关文章推荐
- Android笔记(五十五) Android四大组件之一——ContentProvider,使用系统提供的ContentProvider
- Android使用MediaRecorder实现录音及播放
- Android使用MediaRecorder实现录音功能
- I2C-Tools 4.0使用说明及android平台移植,提供源码下载路径
- Android 使用MediaRecorder录音调用stop()方法的时候报错
- Android 使用mediaRecorder进行录音
- 使用Android自带Gallery组件实现CoverFlow,源码+解析
- 使用Android自带Gallery组件实现CoverFlow,源码+解析
- 浅析Android四大组件之一:ContentProvider 内容提供者 源码
- 使用Android自带Gallery组件实现CoverFlow,源码+解析
- Android 使用MediaRecorder录音调用stop()方法的时候报错【转】
- Android使用MediaRecorder实现录音功能相关知识
- .Net 转战 Android 4.4 日常笔记(9)--常用组件的使用方法[附源码]
- [android ui]侧边栏(使用Support Library 4提供的扩展组件)
- Android录音--AudioRecord、MediaRecorder的使用
- Android使用MediaRecorder实现录音功能
- Android 侧边栏(使用Support Library 4提供的扩展组件)
- 使用Android自带Gallery组件实现CoverFlow,源码+解析
- Android 源码中添加自定义服务并提供给上层使用
- [置顶] Android使用MediaRecorder实现录音及播放