Unity调用Android录音

# Unity调用Android录音

package cn.net.xuefei.record;
import java.io.IOException;
import com.unity3d.player.UnityPlayerActivity;
import android.media.MediaRecorder;
import android.os.Bundle;

public class MainActivity extends UnityPlayerActivity {
    private static MediaRecorder recorder;
    public static MainActivity mainActivity;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity = this;
    }

    public static void Start() {

        mainActivity.runOnUiThread(new Runnable() {
            public void run() {
                recorder = new MediaRecorder();// new出MediaRecorder对象
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                // 设置MediaRecorder的音频源为麦克风
                recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
                // 设置MediaRecorder录制的音频格式
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                // 设置MediaRecorder录制音频的编码为amr.
                recorder.setOutputFile("/sdcard/peipei.amr");
                // 设置录制好的音频文件保存路径
                try {
                    recorder.prepare();// 准备录制
                    recorder.start();// 开始录制
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void Stop() {
        mainActivity.runOnUiThread(new Runnable() {
            public void run() {
                recorder.stop();// 停止刻录
                // recorder.reset(); // 重新启动MediaRecorder.
                recorder.release(); // 刻录完成一定要释放资源
                // recorder = null;
            }
        });
    }
} 

你可能感兴趣的:(Unity,Android)