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 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 亚洲av无码不卡一区二区三区| 精品日韩一区二区| 亚洲av成人一区二区三区观看在线| 成人精品视频一区二区| 中文日韩字幕一区在线观看| 无码国产精品一区二区免费虚拟VR| 国产在线观看一区二区三区精品| 日韩精品电影一区| 亚洲av综合av一区二区三区| 91精品福利一区二区三区野战| 在线精品动漫一区二区无广告| 国产成人一区二区三区免费视频| 亚洲AV成人一区二区三区AV| 中文字幕aⅴ人妻一区二区| 性色AV 一区二区三区| 国产精品一区二区香蕉| 精品国产一区二区三区香蕉事| 国产一区二区三区乱码在线观看| 无码夜色一区二区三区| 亚洲AV无码一区二区三区网址 | 亚洲Aⅴ无码一区二区二三区软件| 亚洲AV无码一区二区三区牲色 | 精品国产福利第一区二区三区| 日本免费精品一区二区三区| 日韩少妇无码一区二区三区| 一级特黄性色生活片一区二区| 一级特黄性色生活片一区二区| 亚洲AV成人精品日韩一区18p| 亚洲美女高清一区二区三区| 中文字幕一区二区免费| 国产成人一区二区三区在线观看 | 国产凸凹视频一区二区| 日韩在线一区二区三区免费视频| 欧洲精品一区二区三区在线观看| 一区二区不卡久久精品| 中文字幕AV一区中文字幕天堂| 极品人妻少妇一区二区三区| 亚洲AV网一区二区三区| 久久亚洲中文字幕精品一区| 久久免费区一区二区三波多野| 国内自拍视频一区二区三区|