Android MediaRecorder示例含代碼

Android MediaRecorder示例

MediaRecorder 類可用于記錄音頻和視頻文件。愛掏網 - it200.com

錄制媒體后,我們可以創建一個可以稍后播放的聲音文件。愛掏網 - it200.com

在這個示例中,我們將 記錄音頻文件 并將其存儲在外部目錄中,格式為3gp。愛掏網 - it200.com

activity_main.xml

從面板拖動2個按鈕,一個用于開始錄制,另一個用于停止錄制。愛掏網 - it200.com在這里,我們使用 android:onClick 在xml文件中將視圖注冊到監聽器上。愛掏網 - it200.com

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="68dp"
        android:layout_marginTop="50dp"
        android:text="Start Recording"
        android:onClick="startRecording"
         />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="64dp"
        android:text="Stop Recording" 
        android:onClick="stopRecording"
        />

</RelativeLayout>

Activity類

package com.javatpoint.mediarecorder;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
      MediaRecorder recorder;
      File audiofile = null;
      static final String TAG = "MediaRecording";
      Button startButton,stopButton;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startButton = (Button) findViewById(R.id.button1);
        stopButton = (Button) findViewById(R.id.button2);
      }

      public void startRecording(View view) throws IOException {
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
            //Creating file
        File dir = Environment.getExternalStorageDirectory();
        try {
          audiofile = File.createTempFile("sound", ".3gp", dir);
        } catch (IOException e) {
          Log.e(TAG, "external storage access error");
          return;
        }
            //Creating MediaRecorder and specifying audio source, output format, encoder & output format
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(audiofile.getAbsolutePath());
        recorder.prepare();
        recorder.start();
      }

      public void stopRecording(View view) {
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
            //stopping recorder
        recorder.stop();
        recorder.release();
           //after stopping the recorder, create the sound file and add it to media library.
        addRecordingToMediaLibrary();
      }

      protected void addRecordingToMediaLibrary() {
            //creating content values of size 4
        ContentValues values = new ContentValues(4);
        long current = System.currentTimeMillis();
        values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
        values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
        values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
        values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());

             //creating content resolver and storing it in the external content uri
        ContentResolver contentResolver = getContentResolver();
        Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Uri newUri = contentResolver.insert(base, values);

           //sending broadcast message to scan the media file so that it can be available
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
        Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
      }
    } 

下載這個 MediaRecorder 示例

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 色欲香天天天综合网站| 四虎成人精品在永久免费| 免费a级黄色片| 一级看片免费视频囗交| 视频一区精品自拍| 日本高清xxx| 国产帅男男gay网站视频| 哒哒哒免费视频观看在线www| 久久夜色精品国产网站| 91香蕉视频直播| 男人桶女人视频30分钟看看吧| 成人午夜又粗又硬有大| 国产亚洲sss在线播放| 久久久久无码中| 适合男士深夜看的小说软件| 日本伦理电影网伦理在线电影| 国产免费一区二区三区免费视频 | 亚洲欧美成人综合久久久| WWW四虎最新成人永久网站| 男女一进一出抽搐免费视频| 女人隐私秘视频黄www免费| 人妻少妇无码精品视频区 | 无码国模国产在线观看| 国产乱子伦视频在线观看| 久久久久一级片| 色哟哟精品视频在线观看| 成人免费的性色视频| 六月丁香婷婷天天在线| eeuss影院www天堂免费| 波多野结衣与老人| 成人看的一级毛片| 免费无码黄十八禁网站在线观看 | 亚洲欧美综合乱码精品成人网| 91精品视频播放| 欧美乱大交xxxx| 国产成人av在线免播放观看| 久久亚洲国产成人精品性色| 亚洲色图五月天| 欧美在线综合视频| 国产愉拍精品视频手机| 中文字幕人成乱码熟女|