2015-02-12 5 views
0

Я хочу получить речевые записи в WAV-файле в формате pcm в Android eclipse. Я вижу данные в sdcard0, но я не могу слушать. Поэтому я удивляюсь, что я спасся или нет. Я читал, что я должен добавить куски, а именно: заголовок RIFF, FMT и DATA. Как я могу добавить? Если кто-нибудь знает, как я могу это сделать, я буду признателен. Пожалуйста, дайте мне несколько советов. Спасибо. «Аудиозапись в формате pcm Android eclipse

public static final int FREQUENCY = 44100; 
public static final int CHANNEL_CONFIGURATION = AudioFormat.CHANNEL_CONFIGURATION_MONO; 
public static final int AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT; 
public String folder_main = "Rize.wav"; 

и его коды похожи на эти;

enter coprivate void recordSound(){ 
     File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+ folder_main); 

     // 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); 
         DataOutputStream dos = new DataOutputStream(bos); 



         // Create a new AudioRecord object to record the audio. 
         int bufferSize = AudioRecord.getMinBufferSize(FREQUENCY, CHANNEL_CONFIGURATION, AUDIO_ENCODING); 
         AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, CHANNEL_CONFIGURATION, AUDIO_ENCODING, bufferSize); 
          short[] buffer = new short[bufferSize]; 
         audioRecord.startRecording(); 
         isRecording = true; 

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


         audioRecord.stop(); 
         audioRecord.release(); 
         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(); 
       } 

    }de here 
+0

header doc - http://www.topherlee.com/software/pcm-tut-wavformat.html 4.1 или больше имеет кодеки для wav. если u правильно получить заголовок на файле, он должен сыграть –

+0

Как я могу добавить этот заголовок в eclipse? – Gianna

+0

http://alvinalexander.com/java/jwarehouse/android/core/java/android/speech/srec/WaveHeader.java.shtml –

ответ

1

У меня была подобная необходимость, нашел это, и она работала:

Метод ниже принимает ваш pcmdata как короткий массив, добавляет правильный заголовок WAV (в зависимости от вашей скорости выборки, количество каналы и битовый формат) и записывается в файл dataoutstream (файл).

ex // PCMtoFile (myDataOutStream, myPCMShortArray, 44100, 2, 16);

public static void PCMtoFile(OutputStream os, short[] pcmdata, int srate, int channel, int format) throws IOException { 
    byte[] header = new byte[44]; 
    byte[] data = get16BitPcm(pcmdata); 

    long totalDataLen = data.length + 36; 
    long bitrate = srate * channel * format; 

    //Marks the file as a riff file. Characters are each 1 byte long. 
    header[0] = 'R'; 
    header[1] = 'I'; 
    header[2] = 'F'; 
    header[3] = 'F'; 
    //Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you'd fill this in after creation. 
    header[4] = (byte) (totalDataLen & 0xff); 
    header[5] = (byte) ((totalDataLen >> 8) & 0xff); 
    header[6] = (byte) ((totalDataLen >> 16) & 0xff); 
    header[7] = (byte) ((totalDataLen >> 24) & 0xff); 
    //File Type Header. For our purposes, it always equals "WAVE". 
    header[8] = 'W'; 
    header[9] = 'A'; 
    header[10] = 'V'; 
    header[11] = 'E'; 
    //Format chunk marker. Includes trailing null 
    header[12] = 'f'; 
    header[13] = 'm'; 
    header[14] = 't'; 
    header[15] = ' '; 
    //Length of format data as listed above 
    header[16] = (byte) format; 
    header[17] = 0; 
    header[18] = 0; 
    header[19] = 0; 
    //Type of format (1 is PCM) - 2 byte integer   
    header[20] = 1; 
    header[21] = 0; 
    //Number of Channels - 2 byte integer 
    header[22] = (byte) channel; 
    header[23] = 0; 
    // Sample Rate - 32 byte integer. Common values are 44100 (CD), 48000 (DAT). 
    //Sample Rate = Number of Samples per second, or Hertz. 
    header[24] = (byte) (srate & 0xff); 
    header[25] = (byte) ((srate >> 8) & 0xff); 
    header[26] = (byte) ((srate >> 16) & 0xff); 
    header[27] = (byte) ((srate >> 24) & 0xff); 
    // (Sample Rate * BitsPerSample * Channels)/8. 
    header[28] = (byte) ((bitrate/8) & 0xff); 
    header[29] = (byte) (((bitrate/8) >> 8) & 0xff); 
    header[30] = (byte) (((bitrate/8) >> 16) & 0xff); 
    header[31] = (byte) (((bitrate/8) >> 24) & 0xff); 
    //(BitsPerSample * Channels)/8.1 - 8 bit mono2 - 
    //8 bit stereo/16 bit mono4 - 16 bit stereo 
    header[32] = (byte) ((channel * format)/8); 
    header[33] = 0; 
    //Bits per sample 
    header[34] = 16; 
    header[35] = 0; 
    //"data" chunk header. Marks the beginning of the data section. 
    header[36] = 'd'; 
    header[37] = 'a'; 
    header[38] = 't'; 
    header[39] = 'a'; 
    // Size of the data section. 
    header[40] = (byte) (data.length & 0xff); 
    header[41] = (byte) ((data.length >> 8) & 0xff); 
    header[42] = (byte) ((data.length >> 16) & 0xff); 
    header[43] = (byte) ((data.length >> 24) & 0xff); 
    //write the byte form header 
    os.write(header, 0, 44); 
    //write the byte form data 
    os.write(data); 
    //close the output stream 
    os.close(); 
} 
Смежные вопросы