Troubles with AudioRecord

All your problems with Audio, Video and Images.

Troubles with AudioRecord

Postby mrco » Thu Mar 25, 2010 2:30 pm

Hi, i'm trying to record sounds from microphone. I've make Recodrder class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.mixer01;
  3.  
  4.  
  5.  
  6. import java.io.BufferedOutputStream;
  7.  
  8. import java.io.DataOutputStream;
  9.  
  10. import java.io.File;
  11.  
  12. import java.io.FileOutputStream;
  13.  
  14. import java.io.OutputStream;
  15.  
  16.  
  17.  
  18. import android.media.AudioFormat;
  19.  
  20. import android.media.AudioRecord;
  21.  
  22. import android.media.MediaRecorder;
  23.  
  24. import android.media.AudioRecord.OnRecordPositionUpdateListener;
  25.  
  26. import android.util.Log;
  27.  
  28.  
  29.  
  30. public class Recorder implements OnRecordPositionUpdateListener
  31.  
  32. {
  33.  
  34.         private static final int  encoding = AudioFormat.ENCODING_PCM_16BIT;
  35.  
  36.         public static final int frequencies[] = {44100,22050,11025,8000};
  37.  
  38.         public int frequency = Recorder.frequencies[0];
  39.  
  40.         public int channels = AudioFormat.CHANNEL_CONFIGURATION_MONO;
  41.  
  42.         private int buffer_size;
  43.  
  44.         public boolean isProcessing;
  45.  
  46.         private String filename;
  47.  
  48.         private OutputStream os;
  49.  
  50.         private BufferedOutputStream bos;
  51.  
  52.         private DataOutputStream dos;
  53.  
  54.         private byte[] buffer;
  55.  
  56.         private AudioRecord recorder;
  57.  
  58.         public Object record_finished = new Object();
  59.  
  60.        
  61.  
  62.         public Recorder( String filename )
  63.  
  64.         {
  65.  
  66.                 try
  67.  
  68.                 {
  69.  
  70.                         this.filename = filename;
  71.  
  72.                 }
  73.  
  74.                 catch (Exception e)
  75.  
  76.                 {
  77.  
  78.                         Log.e("com.mixer01.Recorder", e.toString());
  79.  
  80.                 }
  81.  
  82.         }
  83.  
  84.        
  85.  
  86.         public void start()
  87.  
  88.         {
  89.  
  90.                 isProcessing = true;
  91.  
  92.                 super.start();
  93.  
  94.         }
  95.  
  96.        
  97.  
  98.         /**
  99.  
  100.          * Create AudioRecord object with specified frequency
  101.  
  102.          * @return
  103.  
  104.          */
  105.  
  106.         private AudioRecord createRecorder()
  107.  
  108.         {
  109.  
  110.                 AudioRecord res = null;
  111.  
  112.                 try
  113.  
  114.                 {
  115.  
  116.                     // Create a new AudioRecord object to record the audio.
  117.  
  118.                     buffer_size = AudioRecord.getMinBufferSize(frequency,
  119.  
  120.                                 channels,  encoding);
  121.  
  122.                     res = new AudioRecord(
  123.  
  124.                                 MediaRecorder.AudioSource.MIC,
  125.  
  126.                     frequency, channels,
  127.  
  128.                     encoding, buffer_size);
  129.  
  130.                        
  131.  
  132.                 }
  133.  
  134.                 catch (Exception e)
  135.  
  136.                 {
  137.  
  138.                         Log.e("com.mixer01.Recorder:createRecorder", e.toString());
  139.  
  140.                 }
  141.  
  142.                 return res;
  143.  
  144.         }
  145.  
  146.        
  147.  
  148.  
  149.  
  150.         private void initOutputFile() throws Exception
  151.  
  152.         {
  153.  
  154.                   File file = new File(filename);
  155.  
  156.                  
  157.  
  158.                   // Delete any previous recording.
  159.  
  160.                   if (file.exists())
  161.  
  162.                     file.delete();
  163.  
  164.  
  165.  
  166.                   // Create the new file.
  167.  
  168.               file.createNewFile();
  169.  
  170.                  
  171.  
  172.               // Create a DataOuputStream to write the audio data into the saved file.
  173.  
  174.                   os = new FileOutputStream(file);
  175.  
  176.                   bos = new BufferedOutputStream(os);
  177.  
  178.                   dos = new DataOutputStream(bos);
  179.  
  180.         }
  181.  
  182.        
  183.  
  184.         private void closeOutputFile() throws Exception
  185.  
  186.         {
  187.  
  188.                 dos.close();
  189.  
  190.                 bos.close();
  191.  
  192.                 os.close();
  193.  
  194.         }
  195.  
  196.        
  197.  
  198.         public void startRecord()
  199.  
  200.         {
  201.  
  202.                   try
  203.  
  204.                   {
  205.  
  206.                           isProcessing = true;
  207.  
  208.                           initOutputFile();
  209.  
  210.                    
  211.  
  212.                           int pos = 0;
  213.  
  214.                             recorder = null;
  215.  
  216.                             while (recorder == null)
  217.  
  218.                             {
  219.  
  220.                                 frequency = frequencies[pos];
  221.  
  222.                                 recorder = createRecorder();
  223.  
  224.                                 pos++;
  225.  
  226.                                 if ( pos >= frequencies.length )
  227.  
  228.                                 {
  229.  
  230.                                         // that's end, no more variants <img src="http://www.anddev.org/images/smilies/sad.png" alt=":(" title="Sad" />
  231.  
  232.                                         break;
  233.  
  234.                                 }
  235.  
  236.                             }
  237.  
  238.                             if (recorder == null)
  239.  
  240.                             {
  241.  
  242.                                 throw new Exception("Can't create AudioRecord object");
  243.  
  244.                             }                      
  245.  
  246.                             buffer = new byte[buffer_size];
  247.  
  248.                             recorder.setRecordPositionUpdateListener(this);
  249.  
  250.                             recorder.setPositionNotificationPeriod(buffer_size/2);
  251.  
  252.                             recorder.startRecording();
  253.  
  254.                             int readed = recorder.read(buffer, 0, buffer_size);
  255.  
  256.                             dos.write(buffer, 0, readed);
  257.  
  258.                   }
  259.  
  260.                   catch (Exception e)
  261.  
  262.                   {
  263.  
  264.                           Log.e("com.mixer01.Recorder:startRecord", e.toString());
  265.  
  266.                   }            
  267.  
  268.         }
  269.  
  270.        
  271.  
  272.         public void stopRecorder()
  273.  
  274.         {
  275.  
  276.                 try
  277.  
  278.                 {
  279.  
  280.                         isProcessing = false;
  281.  
  282.                         if ( recorder != null )
  283.  
  284.                         {
  285.  
  286.                                 recorder.stop();
  287.  
  288.                                 recorder.release();
  289.  
  290.                                 recorder = null;
  291.  
  292.                         }
  293.  
  294.                         //this.start();
  295.  
  296.                 }
  297.  
  298.                 catch (Exception e)
  299.  
  300.                 {
  301.  
  302.                         Log.e("com.mixer01.Recorder:stopRecorder", e.toString());
  303.  
  304.                 }
  305.  
  306.         }      
  307.  
  308.         //-------------------------------------------------------------------------------------------------------
  309.  
  310.         //  OnRecordPositionUpdateListener routines
  311.  
  312.         //-------------------------------------------------------------------------------------------------------
  313.  
  314.         public void onMarkerReached(AudioRecord arg0) {}
  315.  
  316.         public void onPeriodicNotification(AudioRecord arg0)
  317.  
  318.         {
  319.  
  320.                 try
  321.  
  322.                 {
  323.  
  324.                     int readed = recorder.read(buffer, 0, buffer_size);
  325.  
  326.                     dos.write(buffer, 0, readed);
  327.  
  328.                         Log.i("OnPeridoc", "buf[0]="+buffer[0]);
  329.  
  330.                         if (!isProcessing)
  331.  
  332.                         {
  333.  
  334.                                 closeOutputFile();
  335.  
  336.                                 synchronized (record_finished)
  337.  
  338.                                 {
  339.  
  340.                                         record_finished.notify();
  341.  
  342.                                 }
  343.  
  344.                         }
  345.  
  346.                 }
  347.  
  348.                 catch (Exception e)
  349.  
  350.                 {
  351.  
  352.                         Log.e("com.mixer01.Recorder:onPeriodicNotification", e.toString());
  353.  
  354.                 }
  355.  
  356.         }      
  357.  
  358. }
  359.  
  360.  
Parsed in 0.049 seconds, using GeSHi 1.0.8.4

This code works on Samsung i5700, but in log i see huge number of errors:
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
W/AudioFlinger( 1816): write blocked for 51 msecs
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
I/writeFrames(10104): End encoding 439
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
I/OnPeridoc(10104): buf[0]=0
E/ALSALib ( 1816): external/alsa-lib/src/pcm/pcm.c:7231:(snd_pcm_recover) overrun occured
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: buffer overflow
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
I/OnPeridoc(10104): buf[0]=0
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: buffer overflow
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
I/OnPeridoc(10104): buf[0]=-1
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: buffer overflow
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
I/OnPeridoc(10104): buf[0]=-46
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: buffer overflow
I/OnPeridoc(10104): buf[0]=-41
I/writeFrames(10104): enter encoding 786
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: buffer overflow
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
W/AudioFlinger( 1816): write blocked for 49 msecs
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!
E/AudioFlinger( 1816): AudioRecordThread: Frame has some error!!

What does they mean?
Also with the same code on another phone (Motorola droid) my test application halts and in log i get:
[quote]
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
W/AudioRecord( 2228): obtainBuffer timed out (is the CPU pegged?) user=00000000, server=00000000
[quote]
What's going wrong?
mrco
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Jan 15, 2010 11:31 am
Location: 2V Software

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 9 guests