473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 9954
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

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

Similar topics

0
by: MLH | last post by:
Edit, Insert Object & choose Wave Object, the following OLE object thing gets inserted onto the form... Microsoft Sound Recorder Version 5.1 (Build 2600.xpsp2.030422-1633: Service Pack 1)...
6
by: Andrew Banks | last post by:
I own VS.NET and am wondering what else I need to develop for the Smartphone 2002 platform? What languages can I develop for the Smartphone 2002 in and does it support ..NET? Thanks in advance,...
1
by: Ray Ackley | last post by:
I'm experiencing a threading problem that's really perplexing me. I have a multithreaded application that reads car ECU information that's sent over the serial port by the ECU and received by the...
1
by: wina | last post by:
Hai, anyone know how to Load a Wav file and put the loaded file as an input for FFT function? I'm building application for SmartPhone using Vb.Net2003 and OpenNETCF library v1.4. It's a violin...
2
by: Abubakar | last post by:
Hi, I want my app's user to be able to record sound through my app and save it to the/some file. How can I do that? Are there any classes in the .net framework that let us do this? Or will I...
4
by: Darhl Thomason | last post by:
I'm developing an app for a Smartphone and want to add a text box that only accepts numbers (much like in the contacts, the phone # fields only accept numbers). Thanks! Darhl
2
by: Ian | last post by:
I am trying to save the current record on a form before opening a report, doesn’t sound to hard does it? The code on a buttons on click event goes like this: DoCmd.DoMenuItem acFormBar,...
11
by: andrewdb | last post by:
I have been working with a database that was already created by somebody else, who now no longer works here, so I cant ask any questions. None the less, there is a table 'Ascertainment' which...
1
by: LTCCTL | last post by:
Hi all, I am creating an application which will send/receive messages, whenever a message comes in it should play a sound file(wave/mid or any other format). How can we do it? I am using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.