2010-08-30 11 views
7

Estoy tratando de grabar un archivo de sonido pcm y reproducirlo. Cuando lo reproduzco, suena lento y toma más tiempo de lo que grabó. No estoy seguro de si el error está en el código de registro o reproducción. Alguna idea de cual es el problema?Android La grabación y la reproducción de audio están corruptas

he copiado en gran medida el código de este ejemplo: http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html

Aquí es el código de registro (bandera isRecording se establece mediante el botón de parada en hilo de interfaz gráfica de usuario).

 android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); 
     int sampleRateInHz = 8000;//8000 44100, 22050 and 11025 
     int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO; 
     int audioFormat = AudioFormat.ENCODING_PCM_16BIT; 

     File sd = Environment.getExternalStorageDirectory(); 
     File file = new File(sd, "msg.wav"); 

     if (file.exists()) 
      file.delete(); 

     try { 
      file.createNewFile(); 
     } catch (IOException e) { 
      Log.e("create file:", e.toString()); 
     } 

     try { 

      OutputStream os = new FileOutputStream(file); 
      BufferedOutputStream bos = new BufferedOutputStream(os); 
      DataOutputStream dos = new DataOutputStream(bos); 

      int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz,channelConfig, audioFormat); 
      short[] buffer = new short[bufferSize]; 
      audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 
        sampleRateInHz,channelConfig, audioFormat,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]); 
       } 
      } 
      dos.close(); 

Este es el código de juego.

  File file = new File(SendAlert.voiceFile); 
      // Get the length of the audio stored in the file (16 bit so 2 bytes per short) 
      // and create a short array to store the recorded audio. 
      int musicLength = (int)(file.length()/2); 
      short[] music = new short[musicLength]; 


      try { 
      // Create a DataInputStream to read the audio data back from the saved file. 
      InputStream is = new FileInputStream(file); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      DataInputStream dis = new DataInputStream(bis); 

      // Read the file into the music array. 
      int i = 0; 
      while (dis.available() > 0) { 
       music[i] = dis.readShort(); 
       i++; 
      } 


      // Close the input streams. 
      dis.close();  


      // Create a new AudioTrack object using the same parameters as the AudioRecord 
      // object used to create the file. 
      AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 
                8000, 
                AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                AudioFormat.ENCODING_PCM_16BIT, 
                musicLength, 
                AudioTrack.MODE_STREAM); 
      // Start playback 
      audioTrack.play(); 

      // Write the music buffer to the AudioTrack object 
      audioTrack.write(music, 0, musicLength); 

Respuesta

0

el problema estaba cambiando la frecuencia de muestreo de 11025 en el ejemplo a 8000.

0

Puede intentar (grabar y reproducir) en estéreo; si la salida tarda el doble que la entrada, me haría pensar que este es el primer problema. ¿Tal vez a pesar de capturar en mono estás escribiendo un archivo estéreo wav?

Si no es exactamente el doble de una reproducción que de grabación, ¿cuál es la proporción?

+0

interesante es el doble de lo que debería ser ... pero cambiando de reproducción estéreo no funcionaba. sonó realmente extraño. –

+0

Si intenta grabarlo en estéreo, ¿cambia algo? –

Cuestiones relacionadas