AudioRecord对象未初始化

在下面的代码中,我的audioRecord对象没有初始化。我尝试将其移动到该onCreate方法并使其成为全局的。我已记录状态,并返回一个值1,表示可以使用。调试器表示startRecording正在未初始化的对象上调用它。它还说它无法获得音频源。


为什么我会收到这些错误?


    package com.tecmark;


    import java.io.BufferedOutputStream;

    import java.io.DataOutputStream;

    import java.io.File;

    import java.io.FileOutputStream;

    import android.app.Activity;

    import android.media.AudioFormat;

    import android.media.AudioRecord;

    import android.media.MediaRecorder;

    import android.os.Bundle;

    import android.os.Environment;

    import android.util.Log;

    import android.view.View;

    import android.widget.TextView;


    public class recorder extends Activity  {


        private Thread thread;

        private boolean isRecording;

        private AudioRecord recorder;

        private FileOutputStream os;

        private BufferedOutputStream bos;

        private DataOutputStream dos;

        private TextView text;

        private int audioSource = MediaRecorder.AudioSource.MIC;

        private int sampleRate = 22050;

        private int channel = AudioFormat.CHANNEL_CONFIGURATION_MONO;

        private int encoding = AudioFormat.ENCODING_PCM_16BIT;

        private int result = 0;

        private int bufferSize;

        private byte[] buffer;


        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);


            Log.v("onCreate", "layout set, about to init audiorec obj");

            text = (TextView)findViewById(R.id.TextView01);


             bufferSize = AudioRecord.getMinBufferSize(sampleRate,channel,encoding);

             buffer = new byte[bufferSize];


            recorder = new AudioRecord(audioSource, sampleRate,channel,encoding,

                    AudioRecord.getMinBufferSize(sampleRate, channel,encoding));

            Log.i("recorder obj state",""+recorder.getRecordingState());

        }

慕桂英3389331
浏览 610回答 3
3回答

繁花如伊

我有同样的问题,通过推杆解决了<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>进入清单。从Lollipop开始,您还需要具体询问用户的每个权限。因为他们可能已经撤销了他们。确保授予权限。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android