Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android簡單的利用MediaRecorder進行錄音的實例代碼

Android簡單的利用MediaRecorder進行錄音的實例代碼

編輯:關於Android編程

復制代碼 代碼如下:
package com.ppmeet; 

import java.io.IOException; 

import android.app.Activity; 
import android.graphics.PixelFormat; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 

/**
 * class name:TestBasicAudio<BR>
 * class description:Basic Record Audio Demo<BR>
 * 
 * @version 1.00 2011/12/01
 * @author CODYY)peijiangping
 */ 
public class TestBasicAudio extends Activity { 
    private Button button_start; 
    private Button button_stop; 
    private MediaRecorder recorder; 

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        getWindow().setFormat(PixelFormat.TRANSLUCENT);// 讓界面橫屏 
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉界面標題 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        // 重新設置界面大小 
        setContentView(R.layout.main); 
        init(); 
    } 

    private void init() { 
        button_start = (Button) this.findViewById(R.id.start); 
        button_stop = (Button) this.findViewById(R.id.stop); 
        button_stop.setOnClickListener(new AudioListerner()); 
        button_start.setOnClickListener(new AudioListerner()); 
    } 

    class AudioListerner implements OnClickListener { 
        @Override 
        public void onClick(View v) { 
            if (v == button_start) { 
                initializeAudio(); 
            } 
            if (v == button_stop) { 
                recorder.stop();// 停止刻錄 
                // recorder.reset(); // 重新啟動MediaRecorder. 
                recorder.release(); // 刻錄完成一定要釋放資源 
                // recorder = null; 
            } 
        } 

        private void initializeAudio() { 
            recorder = new MediaRecorder();// new出MediaRecorder對象 
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
            // 設置MediaRecorder的音頻源為麥克風 
            recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); 
            // 設置MediaRecorder錄制的音頻格式 
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
            // 設置MediaRecorder錄制音頻的編碼為amr. 
            recorder.setOutputFile("/sdcard/peipei.amr"); 
            // 設置錄制好的音頻文件保存路徑 
            try { 
                recorder.prepare();// 准備錄制 
                recorder.start();// 開始錄制 
            } catch (IllegalStateException e) { 
                e.printStackTrace(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 

        } 
    } 

AndroidMainfest.xml
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.ppmeet" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:name=".TestBasicAudio" 
            android:screenOrientation="landscape" > 
            <intent-filter > 
                <action android:name="android.intent.action.MAIN" /> 

                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
    </application> 
    <!-- 聯網權限 --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- 往SDCard寫入數據權限 --> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <!-- 錄音權限 --> 
    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <!-- 在SDCard中創建與刪除文件權限 --> 
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 

</manifest> 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved