Recording and playing via microphone in windows phone 7

Source: Internet
Author: User
 
// Simulate the XNA framework (any application of xna in wp7 must first simulate this type)
public class XNAAsyncDispatcher : IApplicationService         {             private DispatcherTimer frameworkDispatcherTimer;             public XNAAsyncDispatcher(TimeSpan dispatchInterval)             {                 this.frameworkDispatcherTimer = new DispatcherTimer();                 this.frameworkDispatcherTimer.Tick += new EventHandler(frameworkDispatcherTimer_Tick);                 this.frameworkDispatcherTimer.Interval = dispatchInterval;            }             void IApplicationService.StartService(ApplicationServiceContext context)             {                 this.frameworkDispatcherTimer.Start();            }             void IApplicationService.StopService()            {                 this.frameworkDispatcherTimer.Stop();             }            void frameworkDispatcherTimer_Tick(object sender, EventArgs e)             {                 FrameworkDispatcher.Update();            }      }
Write this. ApplicationLifetimeObjects. Add (new XNAAsyncDispatcher (TimeSpan. FromMilliseconds (50) in the App constructor. When the program is excited, it starts to simulate XNA GameTimer.
// Future code using System; using System. collections. generic; using System. linq; using System. net; using System. windows; using System. windows. controls; using System. windows. documents; using System. windows. input; using System. windows. media; using System. windows. media. animation; using System. windows. shapes; using Microsoft. phone. controls; using System. IO; using Microsoft. xna. framework. audio; namespace Wp7 _ recording {public parti Al class MainPage: PhoneApplicationPage {// Constructor public MainPage () {InitializeComponent (); mic. bufferReady + = Default_BufferReady; SoundEffect. masterVolume = 1.0f;} MemoryStream MS; Microphone mic = Microphone. default; // Wire up an event handler so we can empty the buffer when full // Crank up the volume to max // When the buffer's ready we need to empty it // We' ll copy to a MemoryStr Eam/We cocould push into IsolatedStorage etc void Default_BufferReady (object sender, EventArgs e) {byte [] buffer = new byte [1024]; int bytesRead = 0; while (bytesRead = mic. getData (buffer, 0, buffer. length)> 0) ms. write (buffer, 0, bytesRead);} // The user wants to start recording. if we 've already made // a recording, close that MemoryStream and create a new one. // Start recording on the de Fault device. private void start_Click (object sender, RoutedEventArgs e) {if (MS! = Null) ms. close (); MS = new MemoryStream (); mic. start ();} // The user wants to stop recording. checks the microphone // is stopped. reset the MemoryStream position. // Play back the recording. pitch is based on slider value private void stop_Click (object sender, RoutedEventArgs e) {if (mic. state! = MicrophoneState. stopped) mic. stop (); ms. position = 0; SoundEffect se = new SoundEffect (ms. toArray (), mic. sampleRate, AudioChannels. mono); // se. play (1.0f, (float) slider1.Value, 0.0f); se. play ();}}}

In this example, the UI has two buttons, one start and one stop, and the other is a slide.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.