无法在 Android 中使用 AudioRecord 进行录制

Posted

技术标签:

【中文标题】无法在 Android 中使用 AudioRecord 进行录制【英文标题】:Unable to record using AudioRecord in Android 【发布时间】:2016-09-22 12:58:09 【问题描述】:

我正在开发一个 android 应用程序。在我的应用程序中,我需要使用 AudioRecord 类录制音频。现在我正在创建一个简单的应用程序来测试 AudioRecord 类,因为我以前从未使用过它。我的简单应用是当用户单击“录制”按钮开始继续录制时。单击“停止”时,停止录制。然后“播放”按钮播放录制的音频。但是当我点击“记录”按钮时,我的应用程序给了我错误。

这是我的活动

public class MainActivity extends AppCompatActivity 

    Boolean recording;


    private Button btnPlay,btnStop,btnRecord;
    private String outputFile = null;
    //outputFile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        initViews();
        setUpViews();
    

    private void initialize()
    

    

    private void initViews()
    
        btnPlay = (Button)findViewById(R.id.btn_play);
        btnRecord = (Button)findViewById(R.id.btn_record);
        btnStop = (Button)findViewById(R.id.btn_stop);
    

    private void setUpViews()
    
        btnRecord.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                startRecord();
            
        );

        btnStop.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                recording = false;
                Toast.makeText(getBaseContext(),"Stopped",Toast.LENGTH_SHORT).show();
            
        );

        btnPlay.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                playRecord();
            
        );
    

    private void startRecord()

        File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

        try 
            file.createNewFile();

            OutputStream outputStream = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);

            int minBufferSize = AudioRecord.getMinBufferSize(11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);

            short[] audioData = new short[minBufferSize];

            AudioRecord audioRecord = new AudioRecord(MediaRecorder.Audiosource.MIC,
                    11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    minBufferSize);

            audioRecord.startRecording();

            Toast.makeText(getBaseContext(),"Recording",Toast.LENGTH_SHORT).show();

            while(recording)
                int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
                for(int i = 0; i < numberOfShort; i++)
                    dataOutputStream.writeShort(audioData[i]);
                
            

            audioRecord.stop();
            dataOutputStream.close();

         catch (IOException e) 
            e.printStackTrace();
        

    

    private void playRecord()

        File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

        int shortSizeInBytes = Short.SIZE/Byte.SIZE;

        int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
        short[] audioData = new short[bufferSizeInBytes];

        try 
            InputStream inputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);

            int i = 0;
            while(dataInputStream.available() > 0)
                audioData[i] = dataInputStream.readShort();
                i++;
            

            dataInputStream.close();

            AudioTrack audioTrack = new AudioTrack(
                    AudioManager.STREAM_MUSIC,
                    11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    bufferSizeInBytes,
                    AudioTrack.MODE_STREAM);

            audioTrack.play();
            audioTrack.write(audioData, 0, bufferSizeInBytes);


         catch (FileNotFoundException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        
    


单击录制按钮时引发错误。

这是 Logcat 中的错误:

什么是错误?如何正确使用 AudioRecord?

【问题讨论】:

【参考方案1】:

最后我解决了这个问题。我只需要运行新线程进行录制。

 private void startRecord()

        File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

        try 
            file.createNewFile();

            OutputStream outputStream = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            dataOutputStream = new DataOutputStream(bufferedOutputStream);

            final int minBufferSize = AudioRecord.getMinBufferSize(11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);

            audioData = new short[minBufferSize];

            final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    minBufferSize);

            audioRecord.startRecording();

            Toast.makeText(getBaseContext(),"Recording",Toast.LENGTH_SHORT).show();


            recordingThread = new Thread(new Runnable() 
                public void run() 
                    while(recording)
                        int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
                        for(int i = 0; i < numberOfShort; i++)
                            try
                                dataOutputStream.writeShort(audioData[i]);
                            
                            catch (IOException e)
                            
                                Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                            
                        
                    
                
            , "AudioRecorder Thread");

            audioRecord.stop();
            dataOutputStream.close();

         catch (IOException e) 
            e.printStackTrace();
        

    

【讨论】:

以上是关于无法在 Android 中使用 AudioRecord 进行录制的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Android 中使用 Android Crop 图像裁剪库

无法在 android 中使用 setKeyListener

无法在带有 Kotlin 的 Android 中使用 Autodispose

无法使用 setsmallIcon 在 android studio 中更改图标

无法在 Android 中播放使用 AudioRecord 录制的 pcm 文件

地理围栏在 Android 中完全无法使用吗?