2

这是我在临时文件中录制声音的代码。当我录制声音然后收听播放时,一切顺利,但是当我再次单击播放按钮时,出现此错误:

在此处输入图像描述

我怎么解决这个问题?

代码:

using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using Coding4Fun.Toolkit.Audio;
using Coding4Fun.Toolkit.Audio.Helpers;

namespace AudioRecorder.UserControls
{
public partial class SoundRecorderPanel : UserControl
{

    private MicrophoneRecorder _recorder = new MicrophoneRecorder();
    private List<IsolatedStorageFileStream>  _audioList = new List<IsolatedStorageFileStream>();
    private int _counter;
    public SoundRecorderPanel()
    {
        InitializeComponent();
    }

    private void ButtonRecord_OnChecked(object sender, RoutedEventArgs e)
    {
       _recorder.Start();
    }

    private void ButtonRecord_OnUnchecked(object sender, RoutedEventArgs e)
    {
        _recorder.Stop();  
        SaveTempAudio(_recorder.Buffer);
    }

    private void SaveTempAudio(MemoryStream buffer)
    {

        if (_counter==2)
            return;

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

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

            _counter++;
        }
    }

    private void ButtonPlayBack_OnClick(object sender, RoutedEventArgs e)
    {
        var index = int.Parse(((Button) sender).Tag.ToString());
        var audioPlayer = new MediaElement {AutoPlay = true};

        if (index < _audioList.Count)
        {

                audioPlayer.SetSource(_audioList[index]);
                LayoutRoot.Children.Add(audioPlayer);
                audioPlayer.Play();

        }
    }

}

}

4

2 回答 2

1

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

using (var stream = new IsolatedStorageFileStream(_fileName, FileMode.Open, storageFolder))
{
    playBack.SetSource(stream);
    playBack.Play();
}

使用示例代码: https ://coding4fun.codeplex.com/SourceControl/latest#source/Coding4Fun.Toolkit.Test.WindowsPhone.Common/Samples/Audio.xaml.cs

于 2014-07-15T23:59:55.040 回答
0

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

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

    }
于 2014-07-15T17:09:56.363 回答