Android開發 ---- 兩分鐘寫一個錄音演示軟件

Android開發 ---- 兩分鐘寫一個錄音演示軟


    Android自帶的SoundRecoder軟件寫得很簡單,就3個Java文件,最有特色的還算哪個指針了。這裏並不是要介紹那個個指針的實現過程,其實也簡單,就是一個算法,通過錄音過程中獲取的振幅來實現指針的偏移。
[python] view plaincopy
  1. <span style="font-size:16px;">MediaRecorder.getMaxAmplitude(); // 得到錄音時的最大振幅</span>  

    趕緊上代碼吧,兩分鐘的時間馬上就過了...界面設計很簡單,3個按鈕(開始錄音,停止錄音,播放錄音)。

一、新建工程SoundRecoderDemo

二、main.xml(佈局文件)

[html] view plaincopy
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.         <Button  
  8.             android:id="@+id/start"  
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"  
  11.             android:layout_weight="1"  
  12.             android:text="start" />  
  13.   
  14.         <Button  
  15.             android:id="@+id/stop"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_weight="1"  
  19.             android:text="stop" />  
  20.   
  21.         <Button  
  22.             android:id="@+id/play"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:layout_weight="1"  
  26.             android:text="play" />  
  27.   
  28. </LinearLayout></span>  

三、SoundRecorderActivity(具體錄音實現)

[java] view plaincopy
  1. <span style="font-size:16px;">import java.io.File;  
  2. import java.io.IOException;  
  3.   
  4. import android.app.Activity;  
  5. import android.media.MediaRecorder;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class SoundRecorderActivity extends Activity implements OnClickListener {  
  12.   
  13.     private Button btnStart;  
  14.     private Button btnStop;  
  15.     private Button btnPlay;  
  16.   
  17.     private MediaRecorder mMediaRecorder;  
  18.     private File recAudioFile;  
  19.     private MusicPlayer mPlayer;  
  20.   
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.   
  26.         setupViews();  
  27.     }  
  28.   
  29.     private void setupViews() {  
  30.         btnStart = (Button) findViewById(R.id.start);  
  31.         btnStop = (Button) findViewById(R.id.stop);  
  32.         btnPlay = (Button) findViewById(R.id.play);  
  33.           
  34.         btnStart.setOnClickListener(this);  
  35.         btnStop.setOnClickListener(this);  
  36.         btnPlay.setOnClickListener(this);  
  37.           
  38.         recAudioFile = new File("/mnt/sdcard""new.amr");  
  39.     }  
  40.   
  41.     @Override  
  42.     public void onClick(View v) {  
  43.         switch (v.getId()) {  
  44.         case R.id.start:  
  45.             startRecorder();  
  46.             break;  
  47.         case R.id.stop:  
  48.             stopRecorder();  
  49.             break;  
  50.         case R.id.play:  
  51.             mPlayer = new MusicPlayer(SoundRecorderActivity.this);  
  52.             mPlayer.playMicFile(recAudioFile);  
  53.             break;  
  54.         default:  
  55.             break;  
  56.         }  
  57.     }  
  58.   
  59.     private void startRecorder() {  
  60.         mMediaRecorder = new MediaRecorder();  
  61.         if (recAudioFile.exists()) {  
  62.             recAudioFile.delete();  
  63.         }  
  64.   
  65.         mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);   
  66.         mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);  
  67.         mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);  
  68.         mMediaRecorder.setOutputFile(recAudioFile.getAbsolutePath());  
  69.         try {  
  70.             mMediaRecorder.prepare();  
  71.         } catch (IllegalStateException e) {  
  72.             e.printStackTrace();  
  73.         } catch (IOException e) {  
  74.             e.printStackTrace();  
  75.         }  
  76.           
  77.         mMediaRecorder.start();  
  78.     }  
  79.       
  80.     private void stopRecorder(){  
  81.         if (recAudioFile!=null) {  
  82.             mMediaRecorder.stop();  
  83.             mMediaRecorder.release();  
  84.         }  
  85.     }  
  86. }</span>  

四、播放類(MusicPlayer)

[java] view plaincopy
  1. <span style="font-size:16px;">import java.io.File;  
  2.   
  3. import android.content.Context;  
  4. import android.media.MediaPlayer;  
  5. import android.media.MediaPlayer.OnCompletionListener;  
  6. import android.net.Uri;  
  7. import android.util.Log;  
  8.   
  9. public class MusicPlayer {  
  10.     private final static String TAG = "MusicPlayer";  
  11.     private static MediaPlayer mMediaPlayer;  
  12.     private Context mContext;  
  13.       
  14.     public MusicPlayer(Context context){  
  15.         mContext = context;  
  16.     }  
  17.       
  18.     public void playMicFile(File file){  
  19.         if (file!=null && file.exists()) {  
  20.             Uri uri = Uri.fromFile(file);  
  21.             mMediaPlayer = MediaPlayer.create(mContext, uri);  
  22.             mMediaPlayer.start();   
  23.             mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {  
  24.                   
  25.                 public void onCompletion(MediaPlayer mp) {  
  26.                     //TODO:finish   
  27.                     Log.i(TAG, "Finish");  
  28.                 }  
  29.             });  
  30.         }  
  31.     }  
  32.       
  33.     public void stopPlayer(){  
  34.         if(mMediaPlayer.isPlaying()){  
  35.             mMediaPlayer.stop();  
  36.             mMediaPlayer.release();  
  37.         }  
  38.     }  
  39. }  
  40. </span>  

五、添加錄音權限

[html] view plaincopy
  1. <span style="font-size:16px;"><uses-permission android:name="android.permission.RECORD_AUDIO" />  
  2. <uses-permission android:name="android.permission.WAKE_LOCK" />  
  3. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /></span>  

完成,運行查看效果。代碼很簡單,沒有註釋,感興趣的看API!!

Demo下載地址:http://download.csdn.net/detail/tangcheng_ok/3837435

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章