Android多媒體之錄音

來源:互聯網
上載者:User

標籤:多媒體   android   play   layout   儲存   

錄製音訊步驟:
1、建立錄音對象
2、指定錄音裝置(初始化狀態)
3、設定錄製音訊碼率
4、設定錄製音訊編碼格式
5、設定錄製音頻存放的位置
6、準備錄音(準備狀態)
7、開始錄音
8、停止錄音

9、釋放資源

錄製聲音的許可權
 <uses-permission android:name="android.permission.RECORD_AUDIO"/>



錄製音訊狀態圖

public class MediaRecorderTest extends Activity {private MediaRecorder recorder;private MediaPlayer player;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.recorder);//1、建立錄音對象recorder = new MediaRecorder();//2、指定錄音裝置(初始化狀態)recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//3、設定錄製音訊碼率recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);//4、設定錄製音訊編碼格式recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);//5、設定錄製音頻存放的位置(這裡指定手機sdcard的根目錄下,檔案名稱為music.mp3)recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "music.mp3");try {//6、準備錄音(準備狀態)recorder.prepare();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//對剛才的錄音檔案進行播放,建立播放音頻對象player = new MediaPlayer();try {//設定要播放的音頻檔案player.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/music.mp3");//準備播放音頻(準備狀態)player.prepare();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void start(View view){recorder.start();//7、開始錄音Toast.makeText(this, "正在錄音", 0).show();}public void stop(View view){recorder.stop();//8、停止錄音Toast.makeText(this, "結束錄音,錄音檔案以儲存到sdcard。", 0).show();recorder.release();//9、釋放資源}public void play(View view){player.start();//開始播放剛才錄製的音頻}}

xml檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="錄音"         android:onClick="start"/>    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="停止"        android:onClick="stop" />    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="播放"        android:onClick="play" /></LinearLayout>


Android多媒體之錄音

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.