您的位置:首页 > 其它

windows phone 7 通过麦克风录音,并且播放

2014-03-05 00:17 239 查看
原文:windowsphone7通过麦克风录音,并且播放

//模拟XNA的框架(凡是在wp7中应用xna的都必须先模拟此类)
publicclassXNAAsyncDispatcher:IApplicationService
{
privateDispatcherTimerframeworkDispatcherTimer;
publicXNAAsyncDispatcher(TimeSpandispatchInterval)
{
this.frameworkDispatcherTimer=newDispatcherTimer();
this.frameworkDispatcherTimer.Tick+=newEventHandler(frameworkDispatcherTimer_Tick);
this.frameworkDispatcherTimer.Interval=dispatchInterval;
}
voidIApplicationService.StartService(ApplicationServiceContextcontext)
{
this.frameworkDispatcherTimer.Start();
}
voidIApplicationService.StopService()
{
this.frameworkDispatcherTimer.Stop();
}
voidframeworkDispatcherTimer_Tick(objectsender,EventArgse)
{
FrameworkDispatcher.Update();
}
}

在App构造函数里写
this.ApplicationLifetimeObjects.Add(newXNAAsyncDispatcher(TimeSpan.FromMilliseconds(50)));

当程序激动时就开始模拟XNAGameTimer




//后代代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Animation;
usingSystem.Windows.Shapes;
usingMicrosoft.Phone.Controls;
usingSystem.IO;
usingMicrosoft.Xna.Framework.Audio;

namespaceWp7_录音
{
publicpartialclassMainPage:PhoneApplicationPage
{
//Constructor
publicMainPage()
{
InitializeComponent();
mic.BufferReady+=Default_BufferReady;
SoundEffect.MasterVolume=1.0f;
}

MemoryStreamms;
Microphonemic=Microphone.Default;

//Wireupaneventhandlersowecanemptythebufferwhenfull
//Crankupthevolumetomax

//Whenthebuffer'sreadyweneedtoemptyit
//We'llcopytoaMemoryStream
//WecouldpushintoIsolatedStorageetc
voidDefault_BufferReady(objectsender,EventArgse)
{
byte[]buffer=newbyte[1024];
intbytesRead=0;
while((bytesRead=mic.GetData(buffer,0,buffer.Length))>0)
ms.Write(buffer,0,bytesRead);
}
//Theuserwantstostartrecording.Ifwe'vealreadymade
//arecording,closethatMemoryStreamandcreateanewone.
//Startrecordingonthedefaultdevice.
privatevoidstart_Click(objectsender,RoutedEventArgse)
{
if(ms!=null)
ms.Close();
ms=newMemoryStream();
mic.Start();
}
//Theuserwantstostoprecording.Checksthemicrophone
//isstopped.ResettheMemoryStreamposition.
//Playbacktherecording.Pitchisbasedonslidervalue
privatevoidstop_Click(objectsender,RoutedEventArgse)
{
if(mic.State!=MicrophoneState.Stopped)
mic.Stop();
ms.Position=0;
SoundEffectse=newSoundEffect(ms.ToArray(),mic.SampleRate,AudioChannels.Mono);
//se.Play(1.0f,(float)slider1.Value,0.0f);
se.Play();
}
}
}


本实例UI就两个Button一个start一个stop,还有一个滑动条Silder.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐