MediaRecoder(查看api)录制声音
2015-09-15 14:55
423 查看
主程序
package com.test.mymediarecoder; import android.support.v7.app.ActionBarActivity; import java.io.IOException; import android.app.Activity; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button mButtonRecoder; private Button mButtonPlay; private MediaRecorder recorder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonRecoder = (Button) findViewById(R.id.button_recoder); mButtonRecoder.setOnClickListener(this); mButtonPlay = (Button) findViewById(R.id.button_play); mButtonPlay.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_recoder: //查看api,api有示例 recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/my.3gp"); try { recorder.prepare(); recorder.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.button_play: recorder.stop(); recorder.reset(); recorder.release(); break; default: break; } } }
布局文件
<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" > <Button android:id="@+id/button_recoder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="录制" /> <Button android:id="@+id/button_play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" /> </LinearLayout>
权限的添加
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.mymediarecoder" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
相关文章推荐
- C++ Pointer-to-Member Selector
- 将Vim配置成为一款强大的编辑工具之 vimrc配置
- url 中文乱码问题
- 如何设计一个像人的大脑皮层一样的产品?
- DB2怎么根据开始和结束时间算相隔天数
- 如何找出长期未被使用的索引
- Maven学习记录1——什么是 Maven
- ISLR系列:(1)线性回归 Linear Regression
- 机器学习实战-第2章(k-近邻算法)
- Android studio不能进行预览Rendering Problems问题
- Android 实现变色状态栏
- Java默认修饰符
- java操作文件
- Android 混淆代码总结
- BI之路学习笔记2--SSIS/ETL设计练习三:《DB->定期生成excel表》
- Mysql视图
- linux下vsftpd的安装与配置说明
- SE37 创建PO订单BAPI_PO_CREATE1
- FontFamily 和Font 的区别
- Memcache,Redis,MongoDB(数据缓存系统)方案对比与分析