Android基礎入門教程——9.4 使用MediaRecord錄音

來源:互聯網
上載者:User

標籤:

Android基礎入門教程——9.4 使用MediaRecord錄音

標籤(空格分隔): Android基礎入門教程

本節引言

本節是Android多媒體基本API調用的最後一節,帶來的是MediaRecord的簡單使用,
用法非常簡單,我們寫個例子來熟悉熟悉~

1.使用MediaRecord錄製音頻

運行結果

實現代碼

布局代碼:activity_main.xml

<RelativeLayout 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"    tools:context=".MainActivity">    <Button        android:id="@+id/btn_control"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="開始錄音" /></RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {    private Button btn_control;    private boolean isStart = false;    private MediaRecorder mr = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn_control = (Button) findViewById(R.id.btn_control);        btn_control.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(!isStart){                    startRecord();                    btn_control.setText("停止錄製");                    isStart = true;                }else{                    stopRecord();                    btn_control.setText("開始錄製");                    isStart = false;                }            }        });    }    //開始錄製    private void startRecord(){        if(mr == null){            File dir = new File(Environment.getExternalStorageDirectory(),"sounds");            if(!dir.exists()){                dir.mkdirs();            }            File soundFile = new File(dir,System.currentTimeMillis()+".amr");            if(!soundFile.exists()){                try {                    soundFile.createNewFile();                } catch (IOException e) {                    e.printStackTrace();                }            }            mr = new MediaRecorder();            mr.setAudioSource(MediaRecorder.AudioSource.MIC);  //音頻輸入源            mr.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);   //設定輸出格式            mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);   //設定編碼格式            mr.setOutputFile(soundFile.getAbsolutePath());            try {                mr.prepare();                mr.start();  //開始錄製            } catch (IOException e) {                e.printStackTrace();            }        }    }    //停止錄製,資源釋放    private void stopRecord(){        if(mr != null){            mr.stop();            mr.release();            mr = null;        }    }}

最後別忘了在AndroidManifest.xml中添加下述許可權:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

好的,就是這麼簡單~

2.本節範例程式碼下載

RecordDemo.zip

本節小結:

好的,本節內容非常簡單,就是MediaRecorder的使用而已,大概是整套教程中最精簡的一節
了吧~嘿嘿~

Android基礎入門教程——9.4 使用MediaRecord錄音

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.