Android AudioRecord到文件,然后使用AudioTrack进行播放

Android AudioRecord到文件,然后使用AudioTrack进行播放,第1张

概述基本上,我使用AudioRecord将声音文件录制到sdcard目录中.录制5秒钟,然后使用AudioTrack播放.好吧,对我来说,录制的文件不存在.可能录制失败.录制时我做错了什么?播放部分呢?publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setC

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

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

 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 bufer),您可以在while循环中将其本身复制到播放器对象( player.write(audioData,0,numShortsRead);).您说您的UI线程被卡住了,这可能是因为您给Audio Record线程赋予了更高的优先级.

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

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 @R_403_5138@ "+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();            }        });

还有一件事,如果您的要求是先录制几秒钟然后播放,则在播放时应该重新开始录制,您可以使用带有超时的延迟处理程序线程来执行此 *** 作,在该线程中您可以停止录制并复制缓冲区,然后开始录制.

总结

以上是内存溢出为你收集整理的Android AudioRecord到文件,然后使用AudioTrack进行播放全部内容,希望文章能够帮你解决Android AudioRecord到文件,然后使用AudioTrack进行播放所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1095600.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存