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

时间:2014-07-15 10:19:23

标签: windows-phone-8 windows-phone isolatedstoragefile

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

enter image description here

我该如何解决这个问题?

代码:

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

        }
    }

}

}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我解决了我的问题,这很奇怪,但似乎使用(){}不起作用!我手动处理了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();

    }