Android AudioRecord到文件,然后使用AudioTrack进行播放 [英] Android AudioRecord to File then use AudioTrack for Playback

查看:114
本文介绍了Android AudioRecord到文件,然后使用AudioTrack进行播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我使用AudioRecord将声音文件记录到sdcard目录中。录制5秒钟,然后使用AudioTrack播放。

Basically, I use AudioRecord to record a sound file to sdcard directory. It records for 5 seconds and then playback using AudioTrack.

对我来说,记录的文件不存在。可能录制失败。录制时我做错了什么?回放部分呢?

Well, for me, the recorded file doesn't exist. Maybe it didn't record successfully. Anything I did wrong in recording? what about the playback portion?

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    int minBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
            AudioFormat.ENCODING_PCM_16BIT);

  audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM); 


  recordSound();
 }



private void recordSound(){
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"recordsound");
    //    /mnt/sdcard/recordsound
    // Delete any previous recording.
    if (file.exists())
            file.delete();

    try {
                    file.createNewFile();

                    // Create a DataOuputStream to write the audio data into the saved file.
                    OutputStream os = new FileOutputStream(file);
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                     dos = new DataOutputStream(bos);



                    // Create a new AudioRecord object to record the audio.
                    int bufferSize = audioRecord.getMinBufferSize(FREQUENCY, CHANNEL_CONFIGURATION, AUDIO_ENCODING);
                     audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, CHANNEL_CONFIGURATION, AUDIO_ENCODING, bufferSize);

                    short[] buffer = new short[bufferSize]; 
                    audioRecord.startRecording();

                    new Timer().schedule(stop(), 5000);  //time in miliseconds

                  //  while (audioRecord.RECORDSTATE_RECORDING==1) 
                    {
                            int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
                            for (int i = 0; i < bufferReadResult; i++)
                                    dos.writeShort(buffer[i]);
                    }



                   // dos.close();
            } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }

}

private TimerTask stop()
{
    TimerTask t = new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            audioTrack.stop();
            audioTrack.release();
            try {
                dos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



             try {
                    playfilesound();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



        }
    };
    return t;

}


private void playfilesound() throws IOException
{




    int count = 512 * 1024; // 512 kb
    //Reading the file..
    byte[] byteData = null; 
    File file = null; 
    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"recordsound");    //filePath

     if (file.exists())
     {
         int a=0;


     }

    byteData = new byte[(int)count];
    FileInputStream in = null;
    try {
    in = new FileInputStream( file );

    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    int bytesread = 0, ret = 0;
    int size = (int) file.length();
    audioTrack.play();


    while (bytesread < size) {    // Write the byte array to the track 
        ret = in.read( byteData,0, count);   //ret =size in bytes

        if (ret != -1) {
            audioTrack.write(byteData,0, ret);
            bytesread += ret; }  //ret
        else break; }   //while

        in.close(); audioTrack.stop(); audioTrack.release();
    }  


推荐答案

如果需要正在记录它应该播放的声音(意味着循环播放音频),在while循环线程中,您要存储记录的数据(audioData缓冲),您可以在while循环中将其复制到播放器对象(player.write(audioData, 0,numShortsRead);)。您说您的UI线程被卡住了,这可能是因为您给Audio Record线程赋予了更高的优先级。

If your requirement is while it is recording it should play(means looping back audio), In your while loop thread, you are storing the recorded data (audioData bufer), there itself you can copy it to player object with in the while loop (player.write(audioData, 0, numShortsRead);). You said like your UI thread is stuck, it might be because of you are giving more priority to Audio record thread.

检查以下我用于上述回送要求的代码:

Check the below the code which I used for above loop back requirement:

boolean m_isRun=true;
    public void loopback() {
            // Prepare the AudioRecord & AudioTrack
            try {
                buffersize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
                        AudioFormat.ENCODING_PCM_16BIT);

            if (buffersize <= BUF_SIZE) {
                buffersize = BUF_SIZE;
            }
            Log.i(LOG_TAG,"Initializing Audio Record and Audio Playing objects");

            m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, buffersize * 1);

            m_track = new AudioTrack(AudioManager.STREAM_ALARM,
                    SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, buffersize * 1,
                    AudioTrack.MODE_STREAM);

            m_track.setPlaybackRate(SAMPLE_RATE);
        } catch (Throwable t) {
            Log.e("Error", "Initializing Audio Record and Play objects Failed "+t.getLocalizedMessage());
        }

        m_record.startRecording();
        Log.i(LOG_TAG,"Audio Recording started");
        m_track.play();
        Log.i(LOG_TAG,"Audio Playing started");

        while (m_isRun) {
            m_record.read(buffer, 0, BUF_SIZE);
            m_track.write(buffer, 0, buffer.length);
        }

        Log.i(LOG_TAG, "loopback exit");
    }

    private void do_loopback() {
        m_thread = new Thread(new Runnable() {
            public void run() {
                loopback();
            }
        });

另一件事,如果您的要求记录了几秒钟,然后在演奏时演奏记录应该重新开始,您可以使用带有超时的延迟处理程序线程来执行此操作,在该线程中,您可以停止记录复制缓冲区,然后开始记录。

One more thing, If your requirement is record for few seconds and then play, while it is playing your record should start again, you can do that with a delay handler thread with a time out, In that thread you can stop recording copy the buffer, then start recording.

这篇关于Android AudioRecord到文件,然后使用AudioTrack进行播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆