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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cyprus106
    New Member
    • Apr 2008
    • 31

    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!
  • markmcgookin
    Recognized Expert Contributor
    • Dec 2006
    • 648

    #2
    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

    Comment

    • Cyprus106
      New Member
      • Apr 2008
      • 31

      #3
      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!

      Comment

      • markmcgookin
        Recognized Expert Contributor
        • Dec 2006
        • 648

        #4
        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.c s ... 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

        Code:
        VoiceRecorder vr = new VoiceRecorder("myFileName.wav");
        vr.show();
        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.

        Code:
        using System;
        using System.Data;
        using System.Runtime.InteropServices;
        
        #region Attempt 1
        namespace MyNameSpace
        {
            public class VoiceRecorder
            {
                #region API prototypes
                [DllImport("voicectl.dll", EntryPoint = "VoiceRecorder_Create")]
                private unsafe static extern IntPtr VoiceRecorder_Create(CM_VOICE_RECORDER* voicerec);
        
                [DllImport("coredll.dll", EntryPoint = "GetForegroundWindow")]
                private unsafe static extern IntPtr GetForegroundWindow();
                #endregion
        
                [StructLayout(LayoutKind.Sequential)]
                public unsafe struct CM_VOICE_RECORDER
                {
                    public int cb;
                    public wndStyle dwStyle;
                    public int xPos;
                    public int yPos;
                    public IntPtr hwndParent;
                    public int id;
                    public char* lpszRecordFileName;
                }
        
                public enum wndStyle : uint
                {
                    VRS_NO_OKCANCEL = 0x0001, // No OK/CANCLE displayed
                    VRS_NO_NOTIFY = 0x0002, // No parent Notifcation
                    VRS_MODAL = 0x0004, // Control is Modal    
                    VRS_NO_OK = 0x0008, // No OK displayed
                    VRS_NO_RECORD = 0x0010, // No RECORD button displayed
                    VRS_PLAY_MODE = 0x0020, // Immediately play supplied file when launched
                    VRS_NO_MOVE = 0x0040, // Grip is removed and cannot be moved around by the user
                    VRS_RECORD_MODE = 0x0080, // Immediately record when launched
                    VRS_STOP_DISMISS = 0x0100 // Dismiss control when stopped
                }
        
                private unsafe CM_VOICE_RECORDER _VoiceRec;
                private IntPtr _hRecorder;
                private string wavFile = @"\My Documents\VRec_0.wav";
        
                private IntPtr _Hwnd = (IntPtr)0;
        
                public IntPtr Hwnd
                {
                    get { return _Hwnd; }
                    set
                    {
                        _VoiceRec.hwndParent = value;
                        _Hwnd = value;
                    }
                }
        
                public unsafe VoiceRecorder(string _audioFile)
                {
                    wavFile = _audioFile;
                    _hRecorder = new IntPtr();
                    char[] temp = new char[200];
        
                    this.Hwnd = GetForegroundWindow();
        
                    // Populate temp with the file path of the WAV file            
                    Buffer.BlockCopy(wavFile.ToCharArray(), 0, temp, 0, 2 * wavFile.Length);
        
                    fixed (char* lpszFileName = temp)
                    {
                        _VoiceRec = new CM_VOICE_RECORDER();
        
                        _VoiceRec.hwndParent = _Hwnd;
                        _VoiceRec.dwStyle = wndStyle.VRS_NO_MOVE | wndStyle.VRS_MODAL;
                        _VoiceRec.cb = (int)Marshal.SizeOf(_VoiceRec);
                        _VoiceRec.xPos = -1;
                        _VoiceRec.yPos = -1;
                        _VoiceRec.lpszRecordFileName = lpszFileName;
                    }
                }
        
                // Show the voice recorder
                public unsafe void Show()
                {
                    fixed (CM_VOICE_RECORDER* _VoiceRecPtr = &_VoiceRec)
                    {
                        _hRecorder = VoiceRecorder_Create(_VoiceRecPtr);
                    }
                }
            }
        }
        #endregion

        Comment

        • Cyprus106
          New Member
          • Apr 2008
          • 31

          #5
          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!

          Comment

          • markmcgookin
            Recognized Expert Contributor
            • Dec 2006
            • 648

            #6
            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!

            Comment

            • JamesGeddes
              New Member
              • Dec 2009
              • 6

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

              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 'DllImportAttri bute' 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 'DllImportAttri bute' 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 'StructLayoutAt tribute' could not be found (are you missing a using directive or an assembly reference?)

              Any suggestions?

              Thanks!

              James

              Comment

              • markmcgookin
                Recognized Expert Contributor
                • Dec 2006
                • 648

                #8
                do you definitely have

                using System.Runtime. InteropServices ;

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

                Cheers,

                Mark

                Comment

                • JamesGeddes
                  New Member
                  • Dec 2009
                  • 6

                  #9
                  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

                  Comment

                  • Ershad M

                    #10
                    Dear markmcgookin,

                    This code help me a lot and i want to know is it possible to record sound in mp3 format.

                    Comment

                    • Mary F
                      New Member
                      • Jan 2011
                      • 2

                      #11
                      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?

                      Comment

                      • Mary F
                        New Member
                        • Jan 2011
                        • 2

                        #12
                        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

                        Comment

                        • epastorejr
                          New Member
                          • Aug 2006
                          • 1

                          #13
                          Mark, is the source code still available ? If so, where ? Cheers !

                          Comment

                          • markmcgookin
                            Recognized Expert Contributor
                            • Dec 2006
                            • 648

                            #14
                            Just copy and paste the code in the above post into a new class called SoundRecorder.c s and include that into your project...that should be it mate.

                            Mark

                            Comment

                            Working...