Windows phone 8 在windows phone 8上录制临时文件中的声音

Windows phone 8 在windows phone 8上录制临时文件中的声音,windows-phone-8,windows-phone,isolatedstoragefile,Windows Phone 8,Windows Phone,Isolatedstoragefile,这是我在临时文件中录制声音的代码。当我录制声音,然后听回放时,一切都进行得很顺利,但当我再次单击回放按钮时,出现以下错误: 我怎样才能解决这个问题 代码: 使用System.Collections.Generic; 使用System.IO; 使用System.IO.IsolatedStorage; 使用System.Windows; 使用System.Windows.Controls; 使用Coding4Fun.Toolkit.Audio; 使用Coding4Fun.Toolkit.Audio

这是我在临时文件中录制声音的代码。当我录制声音,然后听回放时,一切都进行得很顺利,但当我再次单击回放按钮时,出现以下错误:

我怎样才能解决这个问题

代码:

使用System.Collections.Generic;
使用System.IO;
使用System.IO.IsolatedStorage;
使用System.Windows;
使用System.Windows.Controls;
使用Coding4Fun.Toolkit.Audio;
使用Coding4Fun.Toolkit.Audio.Helpers;
命名空间AudioRecorder.UserControls
{
公共部分类SoundRecorderPanel:UserControl
{
专用麦克风记事本_recorder=新麦克风记事本();
私有列表_audioList=新列表();
私家柜台;;
公共录音机面板()
{
初始化组件();
}
私有无效按钮记录(对象发送方、路由目标方)
{
_recorder.Start();
}
私有无效按钮记录未选中(对象发送器,路由目标e)
{
_录音机。停止();
SaveTempAudio(_recorder.Buffer);
}
私有void SaveTempAudio(内存流缓冲区)
{
如果(_计数器==2)
返回;
使用(IsolatedStorageFile isoStore=IsolatedStorageFile.GetUserStoreForApplication())
{
var bytes=buffer.GetWavAsByteArray(_recorder.SampleRate);
var tempFileName=“tempwaveFile”+\计数器;
IsolatedStorageFileStream audioStream=isoStore.CreateFile(tempFileName);
audioStream.Write(字节,0,字节,长度);
_添加(音频流);
_计数器++;
}
}
private void ButtonPlayBack\u OnClick(对象发送方,路由目标)
{
var index=int.Parse(((按钮)sender.Tag.ToString());
var audioPlayer=新媒体元素{AutoPlay=true};
如果(索引<_audioList.Count)
{
audioPlayer.SetSource(_audioList[index]);
LayoutRoot.Children.Add(音频播放器);
audioPlayer.Play();
}
}
}

}

我解决了我的问题,这很奇怪,但使用(){}似乎不起作用!我手动处理了IsolatedStorageFile和IsolatedStorageFileStream。我还更改了ButtonPlaybackClick事件下的代码。这是我为有同样问题的人编写的新代码

        private void SaveTempAudio(MemoryStream buffer)
    {

        if (_counter == 2)
            return;

        var isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        var bytes = buffer.GetWavAsByteArray(_recorder.SampleRate);
        var tempFileName = "tempwave_" + _counter;
        var audioStream = isoStore.CreateFile(tempFileName);

        audioStream.Write(bytes, 0, bytes.Length);
        _audioList.Add(audioStream);

        _counter++;

        isoStore.Dispose();
        audioStream.Close();
        audioStream.Dispose();
    }

    private void ButtonPlayBack_OnClick(object sender, RoutedEventArgs e)
    {
        var index = int.Parse(((Button) sender).Tag.ToString());
        var fileName = "tempwave_" + ((Button) sender).Tag;

        if (index >= _audioList.Count)
            return;

        var isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
        var fileStream = isoStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read);

        SoundPlayer.SetSource(fileStream);
        SoundPlayer.Play();

        isoStorage.Dispose();
        fileStream.Close();
        fileStream.Dispose();

    }

您可以100%使用using块。问题是您如何在单独的事件中尝试访问流。重新打开它,而不是尝试在流的索引中保存引用

using (var stream = new IsolatedStorageFileStream(_fileName, FileMode.Open, storageFolder))
{
    playBack.SetSource(stream);
    playBack.Play();
}
使用示例代码: