Android recording 录音功能 简单使用小实例
2013-07-22 17:58
651 查看
package com.app.recordingtest; import java.io.File; import java.io.IOException; import android.app.Activity; import android.media.MediaRecorder; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; /** * v1.0 version * * @author Administrator * */ public class MainActivity extends Activity implements OnClickListener { Button button1, button2; MediaRecorder recorder; ListView listView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); listView1 = (ListView) findViewById(R.id.listView1); button1.setOnClickListener(this); button2.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1 : try { begin(); } catch (IOException e) { e.printStackTrace(); } break; case R.id.button2 : stop(); break; default : break; } } private void stop() { recorder.stop(); // 暂停 recorder.reset(); // 重置 recorder.release(); // 释放 Toast.makeText(MainActivity.this, "暂停..", Toast.LENGTH_SHORT).show(); } private void begin() throws IOException { final String PATH = "/mnt/sdcard"; // 设置路径 File filePath = new File(PATH); filePath = File.createTempFile("/fileName", ".amr/", filePath); String outPath = filePath.getPath().toString(); Toast.makeText(MainActivity.this, "开始.." + outPath, Toast.LENGTH_SHORT) .show(); recorder = new MediaRecorder(); // 创建对象 recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置来源 recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // 设置输出格式 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 设置编码 recorder.setOutputFile(outPath); // 设置输出路径 Toast.makeText(MainActivity.this, " .." + filePath.getAbsolutePath().toString(), Toast.LENGTH_SHORT).show(); try { recorder.prepare(); // 准备录音 recorder.start(); // 开始录音 Toast.makeText(MainActivity.this, "开始..", Toast.LENGTH_SHORT) .show(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- Android recording 录音功能 简单使用小实例
- Android中使用GridView和ImageViewSwitcher实现电子相册简单功能实例
- Android 录音与播放功能的简单实例
- Android使用AudioRecord实现暂停录音功能实例代码
- Android Service使用方法--简单音乐播放实例
- Android使用Service实现简单音乐播放实例
- <Android>在Activity被非正常杀死并重建时,使用savedInstanceState进行数据恢复的简单实例
- Android简单的利用MediaRecorder进行录音的实例代码
- Android开发之CheckBox的简单使用与监听功能示例
- 使用Android开发照相功能小程序实例
- android使用Dialog实现复选功能与数据库结合综合实例
- Android实现录音功能实现实例(MediaRecorder)
- Android开发之--(WIFI,蓝牙,电池,背光,SD卡,摄像头,按键,MIC,重力感应等)功能的简单使用
- android简单实例---------ActionBar的简单使用(一)
- Android照相和录音功能的使用
- ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
- android使用Jsoup进行html数据解析简单实例浅析
- Android服务使用方法 - 简单音乐播放实例
- Android开发之--(WIFI,蓝牙,电池,背光,SD卡,摄像头,按键,MIC,重力感应等)功能的简单使用
- Android简单数据存储类SharedPreferences详解及实例(通过“记住密码”功能学习SharedPreferences)