MediaRecoder(查看api)录制声音

主程序

package com.test.mymediarecoder;

import android.support.v7.app.ActionBarActivity;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    private Button mButtonRecoder;
    private Button mButtonPlay;
    private MediaRecorder recorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButtonRecoder = (Button) findViewById(R.id.button_recoder);
        mButtonRecoder.setOnClickListener(this);
        mButtonPlay = (Button) findViewById(R.id.button_play);
        mButtonPlay.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button_recoder:
            //查看api,api有示例
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/my.3gp");
            try {
                recorder.prepare();
                recorder.start();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            break;
        case R.id.button_play:
            recorder.stop();
            recorder.reset();
            recorder.release();
            break;
        default:
            break;
        }

    }

}

布局文件

<LinearLayout 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"
    android:orientation="vertical"
     >

    <Button
        android:id="@+id/button_recoder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="录制" />
    <Button
        android:id="@+id/button_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止" />
</LinearLayout>

权限的添加

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.mymediarecoder"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
好的,下面是一个使用MediaRecorder录制音频的简单Demo: 1. 在AndroidManifest.xml中添加录音权限: ```xml <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 在布局文件中添加一个按钮,用于开始录制音频: ```xml <Button android:id="@+id/start_recording_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Recording" /> ``` 3. 在Activity中添加如下代码: ```java private MediaRecorder mediaRecorder; private String audioFilePath; private Button startRecordingButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startRecordingButton = findViewById(R.id.start_recording_button); startRecordingButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startRecording(); } }); } private void startRecording() { // 创建音频文件 File audioFile = null; try { audioFile = createAudioFile(); } catch (IOException e) { e.printStackTrace(); } // 初始化MediaRecorder mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setOutputFile(audioFile.getAbsolutePath()); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 开始录制音频 try { mediaRecorder.prepare(); mediaRecorder.start(); startRecordingButton.setText("Stop Recording"); } catch (IOException e) { e.printStackTrace(); } } private void stopRecording() { // 停止录制音频 mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; startRecordingButton.setText("Start Recording"); } private File createAudioFile() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String audioFileName = "AUDIO_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_MUSIC); File audioFile = File.createTempFile(audioFileName, ".3gp", storageDir); audioFilePath = audioFile.getAbsolutePath(); return audioFile; } @Override protected void onStop() { super.onStop(); if (mediaRecorder != null) { mediaRecorder.release(); mediaRecorder = null; } } ``` 这个Demo实现了一个简单的录音功能,点击按钮即可开始录制音频,再次点击按钮即可停止录制录制完成后,音频文件会保存在应用的私有目录中,可以通过audioFilePath获取到音频文件的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值