Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AudioRecord文件,然后使用AudioTrack进行播放_Android_Android Audiorecord - Fatal编程技术网

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

Android AudioRecord文件,然后使用AudioTrack进行播放,android,android-audiorecord,Android,Android Audiorecord,基本上,我使用AudioRecord将声音文件录制到SD卡目录。它录制5秒钟,然后使用AudioTrack播放 对我来说,录制的文件不存在。也许它没有成功录制。我在录音时有没有做错什么?播放部分呢 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); int minBufferSize =

基本上,我使用AudioRecord将声音文件录制到SD卡目录。它录制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();
    }  
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int minBufferSize=AudioTrack.getMinBufferSize(44100,AudioFormat.CHANNEL\u配置\u MONO,
音频格式。编码(PCM(16位);
audioTrack=新的audioTrack(AudioManager.STREAM\u MUSIC,44100,AudioFormat.CHANNEL\u CONFIGURATION\u MONO,
AudioFormat.ENCODING_PCM_16位、minBufferSize、AudioTrack.MODE_STREAM);
录制声音();
}
私人录音带{
File File=新文件(Environment.getExternalStorageDirectory().getAbsolutePath()+“/”+“recordsound”);
///mnt/sdcard/recordsound
//删除以前的任何记录。
if(file.exists())
delete();
试一试{
createNewFile();
//创建DataOutStream以将音频数据写入保存的文件。
OutputStream os=新文件OutputStream(文件);
BufferedOutputStream bos=新的BufferedOutputStream(os);
dos=新数据输出流(bos);
//创建新的AudioRecord对象以录制音频。
int bufferSize=audioRecord.getMinBufferSize(频率、通道配置、音频编码);
audioRecord=新的录音(MediaRecorder.AudioSource.MIC、频率、频道配置、音频编码、缓冲区大小);
short[]buffer=新的short[bufferSize];
录音。开始录制();
新计时器()。计划(停止(),5000);//时间(毫秒)
//while(audioRecord.RECORDSTATE_RECORDING==1)
{
int bufferReadResult=audioRecord.read(buffer,0,bufferSize);
for(int i=0;i
如果您的要求是在录制时播放(即循环播放音频),则在您的while循环线程中,您正在存储录制的数据(audioData bufer),您可以在while循环中将其复制到播放器对象(player.write(audioData,0,numShortsRead);)。你们说你们的UI线程被卡住了,这可能是因为你们给了录音线程更多的优先级

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

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();
            }
        });
布尔m_isRun=true;
公共void环回(){
//准备录音带和音轨
试一试{
buffersize=AudioRecord.getMinBufferSize(采样率,
AudioFormat.CHANNEL\u配置\u单声道,
音频格式。编码(PCM(16位);

如果你能研究一下这个问题吗?