Recording and Reading Audio in the emulator sdk-1.0_r2

All your problems with Audio, Video and Images.

Recording and Reading Audio in the emulator sdk-1.0_r2

Postby SorkarGG » Sun Feb 01, 2009 4:27 pm

Hello,

I work under windows vista.
I have been searching for some time and I can't find a detailled tutorial on how to easily record and then read audio on the emulator under eclipse.

I have yet found these steps:

- You have to install a virtual sd card with mksdcard.exe 1024M sdcard.img

-then run the emulator : emulator.exe -sdcard sdcard.img //where sdimg.iso it is the path to the sdcard

-then run adb.exe : adb push hello.png sdcard/hello.png //just to check if the sdcard is correctly connected

-then you should put :
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
in the androidmanifest.xml

-then there is some code to implement which should look like :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void startRecord() {
  2.  
  3.  
  4.  
  5.           recorder = new MediaRecorder();
  6.  
  7.            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
  8.  
  9.           /*ok so I say audio source is the microphone, is it windows/linux's microphone, or the emulator's? */
  10.  
  11.            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  12.  
  13.           //also available in MPEG_4 I believe
  14.  
  15.            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  16.  
  17.          
  18.  
  19.            recorder.setOutputFile("/sdcard/test.3gpp");
  20.  
  21.            //I never saw that file appear anywhere on the sdcard
  22.  
  23.            recorder.prepare();
  24.  
  25.            recorder.start();
  26.  
  27.  
  28.  
  29.  
  30.  
  31.                }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


-then you should stop with :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. recorder.stop(); // at some point click on stop record button
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


-then you should play it. :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
  2.  
  3. mMediaPlayer.start();  // triggered when you click on play
Parsed in 0.034 seconds, using GeSHi 1.0.8.4




I have also heard about : http://code.google.com/intl/fr/android/ ... tml#sdcard in this page of the manual (~1/4 of the total scroll), there are some informations about "Emulator Startup Options", one of them is about Media -audio <backend>.
I couldn't find much about that backend thing, google didn't said much about it. I still don't know if it's important to the audio recording process.

EDIT : under windows the <backend> is winaudio. (found using emulator -help-audio-in)
But it doesn't work with audio-out and audio-in at the same time for me.
emulator: warning: opening audio output failed

audio-out works alone
I tested it with the sample provided with the sdk when reading ressources. It doesn't when trying to read the sdcard."


However, the adb logcat says this repetitivly when I click on record with audio-in:
D/AudioHardware< 25>: AudioStreamInGeneric::read<0x40308160, 320> from fd 7


The fact is all these steps are pretty blurry to me, and I believe I am not the only android newbie trying to record some sound :)


Anyone knows where we can find a complete tutorial "for dummies" teaching this feature?

Did I make a mistake somewhere?

Any clean and simple source available somewhere?

Any help is of course greatly appreciated

Thanks.
as I said
SorkarGG
Freshman
Freshman
 
Posts: 5
Joined: Sun Feb 01, 2009 3:56 pm

Top

Postby MrSnowflake » Sun Feb 01, 2009 5:20 pm

You safe the recording to: "/sdcard/test.3gpp", but you open a raw resource file to playback. You should open you recorded file, ofcourse...

The message in the log is nothing wrong, it's just reporting, it's reading from some source.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby SorkarGG » Sun Feb 01, 2009 5:27 pm

Oups sorry I've changed that, it was a mistake i did while rewriting the code.
The actual code for reading is :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MediaPlayer mp = new MediaPlayer();
  2.  
  3.             try {
  4.  
  5.                         mp.setDataSource("/sdcard/test.3gpp");         
  6.  
  7.                        
  8.  
  9.                 } catch (IllegalArgumentException e) {
  10.  
  11.                         // TODO Auto-generated catch block
  12.  
  13.                         e.printStackTrace();
  14.  
  15.                 } catch (IllegalStateException e) {
  16.  
  17.                         // TODO Auto-generated catch block
  18.  
  19.                         e.printStackTrace();
  20.  
  21.                 } catch (IOException e) {
  22.  
  23.                         // TODO Auto-generated catch block
  24.  
  25.                         e.printStackTrace();
  26.  
  27.                 }
  28.  
  29.             try {
  30.  
  31.                         mp.prepare();
  32.  
  33.                 } catch (IllegalStateException e) {
  34.  
  35.                         // TODO Auto-generated catch block
  36.  
  37.                         e.printStackTrace();
  38.  
  39.                 } catch (IOException e) {
  40.  
  41.                         // TODO Auto-generated catch block
  42.  
  43.                         e.printStackTrace();
  44.  
  45.                 }
  46.  
  47.             mp.start();
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
as I said
SorkarGG
Freshman
Freshman
 
Posts: 5
Joined: Sun Feb 01, 2009 3:56 pm

Postby MrSnowflake » Sun Feb 01, 2009 5:30 pm

I copied the code from the sdk docs and it just worked in XP and Vista...

What exactly is the problem? Can't you hear/load the recorded audio?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby SorkarGG » Sun Feb 01, 2009 7:54 pm

the problem was that I couldn't read the sound I had recorded or record sound.



It was a problem because I just created a new project containing the minimum required and it works.



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.android;
  3.  
  4.  
  5.  
  6. import java.io.IOException;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.content.ContentValues;
  13.  
  14. import android.media.MediaPlayer;
  15.  
  16. import android.media.MediaRecorder;
  17.  
  18. import android.os.Bundle;
  19.  
  20. import android.view.View;
  21.  
  22. import android.view.View.OnClickListener;
  23.  
  24. import android.widget.Button;
  25.  
  26.  
  27.  
  28.  
  29.  
  30. public class rococo extends Activity implements OnClickListener {
  31.  
  32.        
  33.  
  34.         Button record;
  35.  
  36.         Button stop;
  37.  
  38.         Button play;
  39.  
  40.         MediaRecorder recorder = new MediaRecorder();
  41.  
  42.         ContentValues values = new ContentValues(3);
  43.  
  44.        
  45.  
  46.        
  47.  
  48.     /** Called when the activity is first created. */
  49.  
  50.     @Override
  51.  
  52.     public void onCreate(Bundle savedInstanceState) {
  53.  
  54.         super.onCreate(savedInstanceState);
  55.  
  56.         setContentView(R.layout.main);
  57.  
  58.        
  59.  
  60.        
  61.  
  62.         record = (Button)this.findViewById(R.id.record);
  63.  
  64.         record.setOnClickListener(this);
  65.  
  66.        
  67.  
  68.         stop = (Button)this.findViewById(R.id.stop);
  69.  
  70.         stop.setOnClickListener(this);
  71.  
  72.        
  73.  
  74.         play = (Button)this.findViewById(R.id.play);
  75.  
  76.         play.setOnClickListener(this);
  77.  
  78.     }
  79.  
  80.    
  81.  
  82.    
  83.  
  84.  
  85.  
  86.    
  87.  
  88.    
  89.  
  90.     public void record(){
  91.  
  92.    
  93.  
  94.     recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  95.  
  96.     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  97.  
  98.     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  99.  
  100.     recorder.setOutputFile("/sdcard/test.3gpp");
  101.  
  102.    
  103.  
  104.     recorder.prepare();
  105.  
  106.     recorder.start();
  107.  
  108.     }
  109.  
  110.  
  111.  
  112.     public void stop(){
  113.  
  114.         recorder.stop();
  115.  
  116.         recorder.release();
  117.  
  118.     }
  119.  
  120.    
  121.  
  122.     public void play() throws IllegalArgumentException, IllegalStateException, IOException{
  123.  
  124.         MediaPlayer mp = new MediaPlayer();
  125.  
  126.         mp.setDataSource("/sdcard/test.3gpp");
  127.  
  128.         mp.prepare();
  129.  
  130.         mp.start();
  131.  
  132.     }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.         @Override
  141.  
  142.         public void onClick(View v) {
  143.  
  144.                 // TODO Auto-generated method stub
  145.  
  146.                 if (v==record) {
  147.  
  148.                         record();
  149.  
  150.                 }
  151.  
  152.                 if (v==stop){
  153.  
  154.                         stop();
  155.  
  156.                 }
  157.  
  158.                 if (v==play){
  159.  
  160.                         try {
  161.  
  162.                                 play();
  163.  
  164.                         } catch (IllegalArgumentException e) {
  165.  
  166.                                 // TODO Auto-generated catch block
  167.  
  168.                                 e.printStackTrace();
  169.  
  170.                         } catch (IllegalStateException e) {
  171.  
  172.                                 // TODO Auto-generated catch block
  173.  
  174.                                 e.printStackTrace();
  175.  
  176.                         } catch (IOException e) {
  177.  
  178.                                 // TODO Auto-generated catch block
  179.  
  180.                                 e.printStackTrace();
  181.  
  182.                         }
  183.  
  184.                 }
  185.  
  186.                 }
  187.  
  188. }
  189.  
  190.  
Parsed in 0.046 seconds, using GeSHi 1.0.8.4





Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <RelativeLayout
  4.  
  5. android:id="@+id/widget53"
  6.  
  7. android:layout_width="fill_parent"
  8.  
  9. android:layout_height="fill_parent"
  10.  
  11. xmlns:android="http://schemas.android.com/apk/res/android"
  12.  
  13. >
  14.  
  15. <LinearLayout
  16.  
  17. android:id="@+id/widget54"
  18.  
  19. android:layout_width="213px"
  20.  
  21. android:layout_height="284px"
  22.  
  23. android:orientation="vertical"
  24.  
  25. android:layout_alignParentTop="true"
  26.  
  27. android:layout_centerHorizontal="true"
  28.  
  29. >
  30.  
  31. <Button
  32.  
  33. android:id="@+id/record"
  34.  
  35. android:layout_width="wrap_content"
  36.  
  37. android:layout_height="wrap_content"
  38.  
  39. android:text="Record"
  40.  
  41. android:textStyle="bold"
  42.  
  43. android:layout_gravity="center_horizontal"
  44.  
  45. >
  46.  
  47. </Button>
  48.  
  49. <FrameLayout
  50.  
  51. android:id="@+id/widget57"
  52.  
  53. android:layout_width="fill_parent"
  54.  
  55. android:layout_height="50px"
  56.  
  57. >
  58.  
  59. </FrameLayout>
  60.  
  61. <Button
  62.  
  63. android:id="@+id/stop"
  64.  
  65. android:layout_width="wrap_content"
  66.  
  67. android:layout_height="wrap_content"
  68.  
  69. android:text="Stop"
  70.  
  71. android:textStyle="bold"
  72.  
  73. android:layout_gravity="center_horizontal"
  74.  
  75. >
  76.  
  77. </Button>
  78.  
  79. <FrameLayout
  80.  
  81. android:id="@+id/widget60"
  82.  
  83. android:layout_width="fill_parent"
  84.  
  85. android:layout_height="50px"
  86.  
  87. >
  88.  
  89. </FrameLayout>
  90.  
  91. <Button
  92.  
  93. android:id="@+id/play"
  94.  
  95. android:layout_width="wrap_content"
  96.  
  97. android:layout_height="wrap_content"
  98.  
  99. android:text="Play"
  100.  
  101. android:textStyle="bold"
  102.  
  103. android:layout_gravity="center_horizontal"
  104.  
  105. >
  106.  
  107. </Button>
  108.  
  109. </LinearLayout>
  110.  
  111. </RelativeLayout>
  112.  
  113.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4



the problem was that I hadn't actually really installed the sdcard on the emulator and the recorder didn't like it. I had to put :
-sdcard "c:\users\nico\appdata\local\android\sdk-1.0\sdcard.img" -audio winaudio
in Run>Run Configuration>Target>Additionnal Emulator Command Line Options
the important thing I had forget to consider was to put the complete path to the sdcard and not the path relative to the emulator.
the other mistake was to forget to release the recorded file.


My initial problem which was to record and then play the recorded sound is therefore solved.
Thanks for the help

if you want to try this, just click record/record your stuff/click stop/click play
the quality is poor somehow and there is a lot of noise which is annoying for signal processing but that is another story.
(my final goal is to make a query by humming software on androïd)
as I said
SorkarGG
Freshman
Freshman
 
Posts: 5
Joined: Sun Feb 01, 2009 3:56 pm

Postby MrSnowflake » Mon Feb 02, 2009 9:13 am

Hope you get it to work (the humming thingy).

For windows I didn't need the -audio parameter, just for info.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 20 guests