469,300 Members | 2,165 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

Record & save a sound byte from within smartphone app (C#)

I'm simply trying to record a 30 second sound byte and save it to a predesignated file. I can't seem to find any info on how to go about doing this. Does anybody have any suggestions? They'd be much appreciated!
Nov 8 '08 #1
13 9553
markmcgookin
648 Expert 512MB
Hi,

I have a voicerecorder class that will record a .WAV file in windows mobile using the built in technologies (i.e. no 3rd party software needed at all)

I am at home now, I have the code in the office so I can't get at it until Monday, I'll try and see if I have a backup here... but I am not sure.

Mark
Nov 8 '08 #2
Thanks a whole lot, Mark! I'm not in that much of a hurry anyways.

I gotta say, you are always on the ball, and prompt. Thanks a lot for that!
Nov 9 '08 #3
markmcgookin
648 Expert 512MB
Thanks a whole lot, Mark! I'm not in that much of a hurry anyways.

I gotta say, you are always on the ball, and prompt. Thanks a lot for that!

No worries mate, glad to help!

This is a class called VoiceRecorder.cs ... It's fairly simple to use. It's a slightly modified version of one I found on thecodeproject a while back (I'm VERY sorry, I can't find the original URL) it launches the inbuilt audio recording facilities of Windows Mobile.

You invoke it by using

Expand|Select|Wrap|Line Numbers
  1. VoiceRecorder vr = new VoiceRecorder("myFileName.wav");
  2. vr.show();
  3.  
And it behaves just like a dialogbox.

It took me AGES to try and get something like this working, so it's always nice when we can skip out all that hassle and help eachother.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Runtime.InteropServices;
  4.  
  5. #region Attempt 1
  6. namespace MyNameSpace
  7. {
  8.     public class VoiceRecorder
  9.     {
  10.         #region API prototypes
  11.         [DllImport("voicectl.dll", EntryPoint = "VoiceRecorder_Create")]
  12.         private unsafe static extern IntPtr VoiceRecorder_Create(CM_VOICE_RECORDER* voicerec);
  13.  
  14.         [DllImport("coredll.dll", EntryPoint = "GetForegroundWindow")]
  15.         private unsafe static extern IntPtr GetForegroundWindow();
  16.         #endregion
  17.  
  18.         [StructLayout(LayoutKind.Sequential)]
  19.         public unsafe struct CM_VOICE_RECORDER
  20.         {
  21.             public int cb;
  22.             public wndStyle dwStyle;
  23.             public int xPos;
  24.             public int yPos;
  25.             public IntPtr hwndParent;
  26.             public int id;
  27.             public char* lpszRecordFileName;
  28.         }
  29.  
  30.         public enum wndStyle : uint
  31.         {
  32.             VRS_NO_OKCANCEL = 0x0001, // No OK/CANCLE displayed
  33.             VRS_NO_NOTIFY = 0x0002, // No parent Notifcation
  34.             VRS_MODAL = 0x0004, // Control is Modal    
  35.             VRS_NO_OK = 0x0008, // No OK displayed
  36.             VRS_NO_RECORD = 0x0010, // No RECORD button displayed
  37.             VRS_PLAY_MODE = 0x0020, // Immediately play supplied file when launched
  38.             VRS_NO_MOVE = 0x0040, // Grip is removed and cannot be moved around by the user
  39.             VRS_RECORD_MODE = 0x0080, // Immediately record when launched
  40.             VRS_STOP_DISMISS = 0x0100 // Dismiss control when stopped
  41.         }
  42.  
  43.         private unsafe CM_VOICE_RECORDER _VoiceRec;
  44.         private IntPtr _hRecorder;
  45.         private string wavFile = @"\My Documents\VRec_0.wav";
  46.  
  47.         private IntPtr _Hwnd = (IntPtr)0;
  48.  
  49.         public IntPtr Hwnd
  50.         {
  51.             get { return _Hwnd; }
  52.             set
  53.             {
  54.                 _VoiceRec.hwndParent = value;
  55.                 _Hwnd = value;
  56.             }
  57.         }
  58.  
  59.         public unsafe VoiceRecorder(string _audioFile)
  60.         {
  61.             wavFile = _audioFile;
  62.             _hRecorder = new IntPtr();
  63.             char[] temp = new char[200];
  64.  
  65.             this.Hwnd = GetForegroundWindow();
  66.  
  67.             // Populate temp with the file path of the WAV file            
  68.             Buffer.BlockCopy(wavFile.ToCharArray(), 0, temp, 0, 2 * wavFile.Length);
  69.  
  70.             fixed (char* lpszFileName = temp)
  71.             {
  72.                 _VoiceRec = new CM_VOICE_RECORDER();
  73.  
  74.                 _VoiceRec.hwndParent = _Hwnd;
  75.                 _VoiceRec.dwStyle = wndStyle.VRS_NO_MOVE | wndStyle.VRS_MODAL;
  76.                 _VoiceRec.cb = (int)Marshal.SizeOf(_VoiceRec);
  77.                 _VoiceRec.xPos = -1;
  78.                 _VoiceRec.yPos = -1;
  79.                 _VoiceRec.lpszRecordFileName = lpszFileName;
  80.             }
  81.         }
  82.  
  83.         // Show the voice recorder
  84.         public unsafe void Show()
  85.         {
  86.             fixed (CM_VOICE_RECORDER* _VoiceRecPtr = &_VoiceRec)
  87.             {
  88.                 _hRecorder = VoiceRecorder_Create(_VoiceRecPtr);
  89.             }
  90.         }
  91.     }
  92. }
  93. #endregion
  94.  
Nov 10 '08 #4
Wonderful! Goodness the only thing i had to do to get that code working in it's entirety was to turn on the unsafe code switch. Works like an absolute charm. I never would have figured that out myself. Thanks so much mark!
Nov 11 '08 #5
markmcgookin
648 Expert 512MB
Wonderful! Goodness the only thing i had to do to get that code working in it's entirety was to turn on the unsafe code switch. Works like an absolute charm. I never would have figured that out myself. Thanks so much mark!
No problem pal, had the same thing myself a while back trying to find it, glad to help!
Nov 12 '08 #6
Hi Everyone,

Sorry I'm asking a question about such an old post, but I'm getting the following errors

The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'DllImportAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'StructLayout' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'StructLayoutAttribute' could not be found (are you missing a using directive or an assembly reference?)

Any suggestions?

Thanks!

James
Dec 3 '09 #7
markmcgookin
648 Expert 512MB
do you definitely have

using System.Runtime.InteropServices;

In your code? Is this running on a mobile device?

Cheers,

Mark
Dec 3 '09 #8
Fantastic! That sorts it! Thanks Mark!

Is it possible to embed the recording controls in the form? I'm programming for windows mobile 6, so am currently using the wm6 emulator that comes with visual studio 2008

Thanks

James
Dec 3 '09 #9
Dear markmcgookin,

This code help me a lot and i want to know is it possible to record sound in mp3 format.
Oct 14 '10 #10
Mary F
2
hi. can you please give me source code for recording and saving a .wav file format on windows mobile?
Need help. I am new on this. After I record and need to decode the recorded .wav. I already implement that ...can you please help me with record on a wave file format?
Jan 20 '11 #11
Mary F
2
I will implement the code written above. hope that if import .dll will work on windows mobile also.If someone has a record code where the chunks of a wave file format and buffers and described(and samples...) it would be really helpful. Thank you in advance
Jan 20 '11 #12
Mark, is the source code still available ? If so, where ? Cheers !
Nov 8 '11 #13
markmcgookin
648 Expert 512MB
Just copy and paste the code in the above post into a new class called SoundRecorder.cs and include that into your project...that should be it mate.

Mark
Dec 15 '11 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Andrew Banks | last post: by
1 post views Thread by Ray Ackley | last post: by
1 post views Thread by wina | last post: by
2 posts views Thread by Abubakar | last post: by
4 posts views Thread by Darhl Thomason | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.