Connecting Tech Pros Worldwide Help | Site Map

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

Newbie
 
Join Date: Apr 2008
Posts: 31
#1: Nov 8 '08
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!
markmcgookin's Avatar
Moderator
 
Join Date: Dec 2006
Location: Northern Ireland / England
Posts: 546
#2: Nov 8 '08

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


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
Newbie
 
Join Date: Apr 2008
Posts: 31
#3: Nov 9 '08

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


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!
markmcgookin's Avatar
Moderator
 
Join Date: Dec 2006
Location: Northern Ireland / England
Posts: 546
#4: Nov 10 '08

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


Quote:

Originally Posted by Cyprus106

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.  
Newbie
 
Join Date: Apr 2008
Posts: 31
#5: Nov 11 '08

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


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!
markmcgookin's Avatar
Moderator
 
Join Date: Dec 2006
Location: Northern Ireland / England
Posts: 546
#6: Nov 12 '08

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


Quote:

Originally Posted by Cyprus106

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!
Reply