录音器开发实例

来源:互联网 发布:c语言无限循环 编辑:程序博客网 时间:2024/04/28 08:10

录音器实例

本节通过一个简单的录音程序演示一下MediaRecorder的用法。运行项目chapter7_4,如图7-7所示。录音器包含录制、停止和播放3个按钮,并在按钮的下方提供了一个计时器,记录录音的时间。

 图7-7  录音器运行界面当用户点击"录音"按钮后,创建一个MediaRecorder对象并配置数据源的数据,这里数据源来自麦克风,存储文件格式是3gpp,后缀名为.3ga,音频内容编码是AMR NB。每次录音,系统都是临时指定一个输出路径。RecorderActivity代码如下所示,由于录音与多媒体播放的API相似,请参考源代码注释,这里不再一一讲解。
  1. package com.ophone.chapter7_4;  
  2.  
  3. import java.io.IOException;  
  4.  
  5. import android.app.Activity;  
  6. import android.media.MediaPlayer;  
  7. import android.media.MediaRecorder;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.view.View;  
  12. import android.widget.ImageButton;  
  13. import android.widget.TextView;  
  14.  
  15. public class RecorderActivity extends Activity {  
  16.  
  17.     public static final int UPDATE = 0;  
  18.     private ImageButton play;  
  19.     private ImageButton stop;  
  20.     private ImageButton record;  
  21.     private TextView time;  
  22.     private MediaRecorder recorder;  
  23.     private MediaPlayer player;  
  24.     private String path = "";  
  25.     private int duration = 0;  
  26.     private int state = 0;  
  27.     private static final int IDLE = 0;  
  28.     private static final int RECORDING = 1;  
  29.  
  30.     private Handler handler = new Handler() {  
  31.  
  32.         @Override  
  33.         public void handleMessage(Message msg) {  
  34.             if (state == RECORDING) {  
  35.                 super.handleMessage(msg);  
  36.                 duration++;  
  37.                 time.setText(timeToString());  
  38.                 //循环更新录音器的界面  
  39.                 handler.sendMessageDelayed(handler.
    obtainMessage(UPDATE), 1000);  
  40.             }  
  41.         }  
  42.  
  43.     };  
  44.  
  45.     @Override  
  46.     public void onCreate(Bundle savedInstanceState) {  
  47.         super.onCreate(savedInstanceState);  
  48.         setContentView(R.layout.main);  
  49.         //初始化"播放"按钮  
  50.         play = (ImageButton) findViewById(R.id.play);  
  51.         play.setOnClickListener(new View.OnClickListener() {  
  52.             public void onClick(View arg0) {  
  53.                 play();  
  54.             }  
  55.         });  
  56.         //初始化"停止"按钮  
  57.         stop = (ImageButton) findViewById(R.id.stop);  
  58.         stop.setOnClickListener(new View.OnClickListener() {  
  59.             public void onClick(View arg0) {  
  60.                 stop();  
  61.             }  
  62.         });  
  63.         //初始化"录音"按钮  
  64.         record = (ImageButton) findViewById(R.id.record);  
  65.         record.setOnClickListener(new View.OnClickListener() {  
  66.             public void onClick(View arg0) {  
  67.                 record();  
  68.             }  
  69.         });  
  70.         time = (TextView) findViewById(R.id.time);  
  71.     }  
  72.  
  73.     //播放刚刚录制的音频文件  
  74.     private void play() {  
  75.         if ("".equals(path) || state == RECORDING)  
  76.             return;  
  77.         if (player == null)  
  78.             player = new MediaPlayer();  
  79.         else  
  80.             player.reset();  
  81.         try {  
  82.             player.setDataSource(path);  
  83.             player.prepare();  
  84.             player.start();  
  85.         } catch (IllegalArgumentException e) {  
  86.             e.printStackTrace();  
  87.         } catch (IllegalStateException e) {  
  88.             e.printStackTrace();  
  89.         } catch (IOException e) {  
  90.             e.printStackTrace();  
  91.         }  
  92.     }  
  93.  
  94.     private void record() {  
  95.         try {  
  96.             if (recorder == null)  
  97.                 recorder = new MediaRecorder();  
  98.             //设置输入为麦克风  
  99.             recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
  100.             //设置输出的格式为3gp文件  
  101.             recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
  102.             //音频的编码采用AMR  
  103.             recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
  104.             //临时的文件存储路径  
  105.             path = "/sdcard/" + System.currentTimeMillis() + ".3ga";  
  106.             recorder.setOutputFile(path);  
  107.             recorder.prepare();  
  108.             recorder.start();  
  109.             state = RECORDING;  
  110.             handler.sendEmptyMessage(UPDATE);  
  111.         } catch (IllegalStateException e) {  
  112.             e.printStackTrace();  
  113.         } catch (IOException e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.     }  
  117.  
  118.     private void stop() {  
  119.         // 停止录音,释放recorder对象  
  120.         if (recorder != null) {  
  121.                  recorder.stop();  
  122.                  recorder.release();  
  123.         }  
  124.         recorder = null;  
  125.         handler.removeMessages(UPDATE);  
  126.         state = IDLE;  
  127.         duration = 0;  
  128.     }  
  129.  
  130.     @Override  
  131.     protected void onDestroy() {  
  132.         super.onDestroy();  
  133.         //Activity销毁后,释放播放器和录音器资源  
  134.         if (recorder != null) {  
  135.             recorder.release();  
  136.             recorder = null;  
  137.         }  
  138.         if (player != null) {  
  139.             player.release();  
  140.             player = null;  
  141.         }  
  142.     }  
  143.  
  144.     private String timeToString() {  
  145.         if (duration >= 60) {  
  146.             int min = duration / 60;  
  147.             String m = min > 9 ? min + "" : "0" + min;  
  148.             int sec = duration % 60;  
  149.             String s = sec > 9 ? sec + "" : "0" + sec;  
  150.             return m + ":" + s;  
  151.         } else {  
  152.             return "00:" + (duration > 9 ? duration + "" : "0" + duration);  
  153.         }  
  154.     }  
本文转载自:http://book.51cto.com/art/200912/173826.htm,转载请注明出处

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 绑定银行卡的手机号不用了怎么办 oppo账号密码忘了怎么办 魅族账号密码忘了怎么办 苹果手机账号密码忘了怎么办 oppo手机账号密码忘了怎么办 华为手机账号密码忘了怎么办 vivo手机账号密码忘了怎么办 金立s9密码忘了怎么办 手机密号忘了怎么办 qq号忘了账号怎么办 推特密码忘了怎么办 沃邮箱改密码短信延迟怎么办 被qq好友拉黑了怎么办 网易邮箱字体变小了怎么办 邮箱注册不了全拼怎么办 家用电器接入两相火线后怎么办 手机充电板坏了怎么办 闪电宝商户乱跳怎么办 网易邮箱发错了怎么办 苹果手机邮件无法连接服务器怎么办 好记星平板电脑开不了机怎么办 邮箱里的文件过期了怎么办 小米手机邮件发不了怎么办 有人加我qq邮箱怎么办 文件太大发邮件太慢怎么办 爱又米发信息怎么办 手机上电子邮件己停止运行怎么办 苹果电子邮件密码忘了怎么办 玩游戏时电脑烫怎么办 qq邮箱独立密码忘记了怎么办 qq邮箱中转站容量不足怎么办 qq邮箱忘记密码了怎么办 qq邮箱超大附件过期怎么办 忘记qq邮箱独立密码怎么办 网易邮箱账号忘了怎么办 微信登录密码忘了怎么办 微信太久没登录登录不上怎么办 邮箱独立密码忘记了怎么办 苹果设置id没有邮箱怎么办 苹果手机设置id没有邮箱怎么办 邮箱的附件过期了怎么办