gpt4 book ai didi

windows-phone-7 - Windows Phone 7 录音问题

转载 作者:行者123 更新时间:2023-12-02 04:56:04 26 4
gpt4 key购买 nike

我将致力于我的 Windows Phone 7 应用程序中的录音功能。我通过此引用实现了录音功能 link .

它在那里和我的情况下都完全正常。

实际情况是,在我的应用程序中,我创建了第一个页面,该页面将用作与上述链接相同的录制屏幕。当我们停止录音时,我重定向到第二页并将录音保存在隔离存储中,在第二页我绑定(bind)了录制的声音。在这里我播放了录制的声音,它工作正常。

现在,当我再次转到录制屏幕(第一页)并开始另一次录制时。它有时会录制得很好,有时它会在录制过程中跳过一些声音,就像哔哔声一样,它看起来像是录制中的额外噪音,并且没有正确录制声音。

我的代码是这样的

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);
}
}

所以,请帮我解决这个问题。我在某个地方听说,当您重定向到另一个屏幕时,您必须处理麦克风的所有对象。是真的吗?或其他任何内容。

请帮帮我。展望 future 。

最佳答案

您应该使用以下方式来读取记录的字节,而不是使用集合。这应该在麦克风对象的 BufferReady 事件中完成。

 byte[] audioBuffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
microphone.GetData(audioBuffer);
RecordingStream.Write(audioBuffer, 0, audioBuffer.Length);

RecordingStream 是一个 MemoryStream ,应该全局声明。

我对此不确定,但因为我已经使用过它并且在每种情况下它都工作得很好。试试这个。

关于windows-phone-7 - Windows Phone 7 录音问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22217167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com