这是我在临时文件中录制声音的代码。当我录制声音然后收听播放时,一切顺利,但是当我再次单击播放按钮时,出现此错误:
我怎么解决这个问题?
代码:
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();
}
}
}
}