Windows phone 7 windows phone 7录制问题
我将在我的windows phone 7应用程序中使用录音功能。 我通过这个参考实现了录音功能 它在那里和我的情况下都很好用 实际上,这个场景是,在我的应用程序中,我创建了第一个页面,该页面将作为录制屏幕工作,与上面提到的链接相同。 当我们停止录音时,我重定向到第二页,并将录音保存在单独的存储器中,在第二页,我绑定了录制的声音。在这里,我播放了录制的声音,效果很好 现在,当我再次进入录制屏幕(第一页)并开始另一次录制。它有时会录制得很好,有时会跳过一些录音过程中的声音,比如嘟嘟声,在录音过程中看起来会有额外的噪音,无法正确录制声音 我的代码是Windows phone 7 windows phone 7录制问题,windows-phone-7,microphone,video-recording,Windows Phone 7,Microphone,Video Recording,我将在我的windows phone 7应用程序中使用录音功能。 我通过这个参考实现了录音功能 它在那里和我的情况下都很好用 实际上,这个场景是,在我的应用程序中,我创建了第一个页面,该页面将作为录制屏幕工作,与上面提到的链接相同。 当我们停止录音时,我重定向到第二页,并将录音保存在单独的存储器中,在第二页,我绑定了录制的声音。在这里,我播放了录制的声音,效果很好 现在,当我再次进入录制屏幕(第一页)并开始另一次录制。它有时会录制得很好,有时会跳过一些录音过程中的声音,比如嘟嘟声,在录音过程中看
public partial class NikhilRecord : PhoneApplicationPage
{
//XNA Objects for Record And Playback
Microphone mphone;
//Used for Storing captured buffers
List<byte[]> memobuffercollection = new List<byte[]>();
//Used for displaying stored memos
ObservableCollection<MemoInfo> memofiles = new ObservableCollection<MemoInfo>();
SpaceTime spaceTime = new SpaceTime();
public NikhilRecord()
{
InitializeComponent();
//Create new Microphone and set event handler.
mphone = Microphone.Default;
mphone.BufferReady += OnMicrophoneBufferReady;
String FileName = PhoneApplicationService.Current.State["MySelectedSong"].ToString();
using (IsolatedStorageFile IsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
using (IsolatedStorageFileStream fileStream = IsolatedStorage.OpenFile(FileName, FileMode.Open, FileAccess.Read))
{
MyMedia.SetSource(fileStream);
MyMedia.CurrentStateChanged += new RoutedEventHandler(mediaPlayer_CurrentStateChanged);
fileStream.Close();
fileStream.Dispose();
//Start Recording
OnRecordButtonClick();
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
void UpdateRecording(bool isRecording)
{
if (!isRecording)
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
spaceTime.Space = storage.AvailableFreeSpace;
}
}
else
{
spaceTime.Space = memobuffercollection.Count * mphone.GetSampleSizeInBytes(mphone.BufferDuration);
}
spaceTime.Time = mphone.GetSampleDuration((int)Math.Min(spaceTime.Space, Int32.MaxValue));
}
void OnMicrophoneBufferReady(object sender, EventArgs e)
{
// Get buffer from microphone and add to collection
byte[] buffer = new byte[mphone.GetSampleSizeInBytes(mphone.BufferDuration)];
int bytesreturned = mphone.GetData(buffer);
memobuffercollection.Add(buffer);
UpdateRecording(true);
// To be Continue...
if (spaceTime.Time > TimeSpan.FromMinutes(10))
{
StopRecording();
UpdateRecording(false);
}
}
void OnRecordButtonClick()
{
if (mphone.State == MicrophoneState.Stopped)
{
// Clear the collection for storing the buffers
memobuffercollection.Clear();
// Start Recording
mphone.Start();
MyMedia.Play();
}
else
{
MyMedia.Stop();
//mphone.Stop();
PopUpGrid.Visibility = Visibility.Visible;
RecordGrid.Opacity = 0.5;
RecordGrid.IsHitTestVisible = false;
}
bool isRecording = mphone.State == MicrophoneState.Started;
UpdateRecording(isRecording);
}
void StopRecording()
{
// Get the last partial buffer
int sampleSize = mphone.GetSampleSizeInBytes(mphone.BufferDuration);
byte[] extraBuffer = new byte[sampleSize];
int extraBytes = mphone.GetData(extraBuffer);
// Stop Recording
mphone.Stop();
//Stop the Song
MyMedia.Stop();
// Create MemoInfo object and add at top of collection
int totalSize = memobuffercollection.Count * sampleSize + extraBytes;
TimeSpan duration = mphone.GetSampleDuration(totalSize);
MemoInfo memoInfo = new MemoInfo(DateTime.UtcNow, totalSize, duration);
memofiles.Insert(0, memoInfo);
// Save Data in IsolatedStorage
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
string[] alldirectories = storage.GetDirectoryNames("NikDirectory");
if (alldirectories.Count() == 0)
storage.CreateDirectory("NikDirectory");
try
{
using (IsolatedStorageFileStream stream = storage.CreateFile("NikDirectory\\" + memoInfo.FileName))
{
// Write buffers from collection
foreach (byte[] buffer in memobuffercollection)
stream.Write(buffer, 0, buffer.Length);
// Write partial buffer
stream.Write(extraBuffer, 0, extraBytes);
stream.Close();
stream.Dispose();
}
Uri url = new Uri("/Gallery.xaml", UriKind.Relative);
NavigationService.Navigate(url);
memobuffercollection.Clear();
}
catch (Exception ees)
{
MessageBox.Show(ees.Message);
Uri url = new Uri("/Karaoke.xaml", UriKind.Relative);
NavigationService.Navigate(url);
}
}
bool isRecording = mphone.State == MicrophoneState.Started;
UpdateRecording(isRecording);
}
}
公共部分类记录:PhoneApplicationPage
{
//用于录制和播放的XNA对象
麦克风;
//用于存储捕获的缓冲区
List memobffercollection=新列表();
//用于显示存储的备忘录
ObservableCollection memofiles=新的ObservableCollection();
时空时空=新时空();
公共记录()
{
初始化组件();
//创建新麦克风并设置事件处理程序。
mphone=麦克风。默认值;
mphone.BufferReady+=OnMicrophoneBufferReady;
字符串文件名=PhoneApplicationService.Current.State[“MySelectedSong”].ToString();
使用(IsolatedStorageFile IsolatedStorage=IsolatedStorageFile.GetUserStoreForApplication())
{
尝试
{
使用(IsolatedStorageFileStream fileStream=IsolatedStorage.OpenFile(文件名,FileMode.Open,FileAccess.Read))
{
MyMedia.SetSource(文件流);
MyMedia.CurrentStateChanged+=新路由EventHandler(mediaPlayer\u CurrentStateChanged);
fileStream.Close();
Dispose();
//开始录音
OnRecordButtonClick();
}
}
捕获(异常exc)
{
MessageBox.Show(exc.Message);
}
}
无效更新记录(bool isRecording)
{
如果(!isRecording)
{
使用(IsolatedStorageFile storage=IsolatedStorageFile.GetUserStoreForApplication())
{
spaceTime.Space=storage.AvailableFreeSpace;
}
}
其他的
{
spaceTime.Space=memobffercollection.Count*mphone.GetSampleSizeInBytes(mphone.BufferDuration);
}
spaceTime.Time=mphone.GetSampleDuration((int)Math.Min(spaceTime.Space,Int32.MaxValue));
}
MicrophoneBufferReady上的void(对象发送方,事件参数e)
{
//从麦克风获取缓冲区并添加到集合中
byte[]buffer=新字节[mphone.GetSampleSizeInBytes(mphone.BufferDuration)];
int bytesreturned=mphone.GetData(缓冲区);
memobuffercollection.Add(buffer);
更新记录(真);
//继续。。。
if(spaceTime.Time>TimeSpan.FromMinutes(10))
{
停止录制();
更新记录(假);
}
}
void OnRecordButtonClick()
{
if(mphone.State==麦克风状态.已停止)
{
//清除用于存储缓冲区的集合
memobuffercollection.Clear();
//开始录音
mphone.Start();
MyMedia.Play();
}
其他的
{
MyMedia.Stop();
//mphone.Stop();
PopUpGrid.Visibility=可见性.Visibility;
不透明度=0.5;
RecordGrid.IshittesVisible=false;
}
bool isRecording=mphone.State==MicrophoneState.Started;
更新记录(isRecording);
}
void StopRecording()
{
//获取最后一个部分缓冲区
int sampleSize=mphone.GetSampleSizeInBytes(mphone.BufferDuration);
字节[]外部缓冲区=新字节[sampleSize];
int extraBytes=mphone.GetData(extraBuffer);
//停止录音
mphone.Stop();
//停止这首歌
MyMedia.Stop();
//创建MemoInfo对象并添加到集合顶部
int totalSize=memobffercollection.Count*sampleSize+extraBytes;
TimeSpan duration=mphone.GetSampleDuration(totalSize);
memonifo memonifo=新的memonifo(DateTime.UtcNow、totalSize、duration);
备忘录文件。插入(0,备忘录信息);
//将数据保存在IsolatedStorage中
使用(IsolatedStorageFile storage=IsolatedStorageFile.GetUserStoreForApplication())
{
字符串[]alldirectories=storage.GetDirectoryNames(“NikDirectory”);
if(alldirectories.Count()==0)
CreateDirectory(“NikDirectory”);
尝试
{
使用(IsolatedStorageFileStream=storage.CreateFile(“NikDirectory\\”+memoInfo.FileName))
{
//从集合写入缓冲区
foreach(memobuffercollection中的字节[]缓冲区)
stream.Write(buffer,0,buffer.Length);
//写部分缓冲区
stream.Write(extraBuffer,0,extraBytes);
stream.Close();
stream.Dispose();
}
Uri url=newURI(“/Gallery.xaml”,UriKind.Relative);
导航服务。导航(url);
byte[] audioBuffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
microphone.GetData(audioBuffer);
RecordingStream.Write(audioBuffer, 0, audioBuffer.Length);