播放时如何录制部分语音

时间:2015-09-05 04:12:18

标签: android multithreading audio audio-recording audiotrack

我在android中有关于录音的难题。当我和手机通话时,我用AudioTrack播放声音。如果我按下按钮记录,我想在我的声音中录制一个声部。例如,我会在10秒钟内发言。但是,如果按下录制按钮,我只想录制我的声音。它可以是从第3秒到第8秒。但是,我的手机需要在我讲话时播放我的声音(从第1秒到第10秒)。

目前,我使用一个线程播放我的声音,如下面的代码。我在录制线程中创建了一个标志,以决定何时录制。当我按下按钮记录时,标志将被设置为true。然后我点击停止,它将设置为false并写入文件

    public class AudioSoundThread extends Thread {
    private short[] audioBuffer;    
    private boolean isRecording = false;
    private boolean isSounding = true;
    private AudioRecordingHandler handler = null;
    private AudioRecord record;
    private AudioTrack track;
    public AudioSoundThread(AudioTrack mtrack,AudioRecord mrecord,short[] maudioBuffer, AudioRecordingHandler handler) {
        this.handler = handler;     
        this.audioBuffer = maudioBuffer;        
        this.record=mrecord; 
        this.track=mtrack;
    }

    @Override
    public void run() {

        record.startRecording();
        DataOutputStream output =null;
        if(isRecording){
           output = prepareWriting();
           if (output == null) { return; }
    }
        track.play();
        ///////////Play during recording                
        int readSize =0;
        while (isSound) {
            readSize=record.read(audioBuffer, 0, audioBuffer.length);
            if ((readSize == AudioRecord.ERROR_INVALID_OPERATION) || 
                    (readSize == AudioRecord.ERROR_BAD_VALUE) ||
                    (readSize <= 0)) {
                    continue;
                }
            if(AudioRecord.ERROR_INVALID_OPERATION != readSize){
                track.write(audioBuffer, 0, readSize);
               }
         if(isRecording)
            write(output,readSize);
           //Finished to write
        if(!isRecording&&output!=null)
        {
           finishWriting(output);
            File waveFile = getFile("wav");
            try {
                rawToWave(mRecording, waveFile);
                deleteTempFile(mRecording);
            } catch (IOException e) {
                Log.e("Error writing file : ", e.getMessage());
            }
        }


        }       
        record.stop();
        record.release();

    }
    public synchronized void stopSound() {
        isSound = false;
    }
    public synchronized void startSound() {
        isSound = true;
    }
    public synchronized void startRecordingFlag() {
    isRecording = true;
    }
    public synchronized void stopRecording() {
    isRecording = false;
}
private void finishWriting(DataOutputStream out) {
    try {
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
        if (handler != null) {
            handler.onRecordingError();
        }
    }
}
private DataOutputStream prepareWriting() {

    if (mRecording.exists()) { mRecording.delete(); }      
    DataOutputStream out = null;
    try {
        out =  new DataOutputStream(new BufferedOutputStream(new FileOutputStream(mRecording)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        if (handler != null) {
            handler.onRecordingError();
        }
    }
    return out;
}

在mainActivity中,我有两个按钮播放我的声音和按钮录制

     private AudioSoundThread     recordingThread;
    //Play button
    btnPlay = (Button) findViewById(R.id.btnPlay);
    btnPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startSounding();
        }
    });
    //Record button
    btnRecord = (Button) findViewById(R.id.btnRecord);
    btnRecord.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //record();
            recordingThread.startRecordingFlag();
        }
    });
   //Stop Record button
    btnStopRecord = (Button) findViewById(R.id.btnStopRecord);
    btnStopRecord .setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //record();
            recordingThread.stopRecording();
        }
    });
private void startSounding() {
    soundingThread = new AudioSoundThread(track,mRecorder,mBuffer,new AudioRecordingHandler() { 
    });
    soundingThread.start();
}

但是,我的方案不起作用。我认为我的旗帜无法发送给线程。你能看一下我的代码并给我一个解决方案吗?

0 个答案:

没有答案