// 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.