.net麦克风记录保存到Windows Phone 8中的文件

时间:2014-04-16 21:06:28

标签: c# .net windows-phone-7 windows-phone-8 xna

我需要知道如何在录制后立即将声音流保存在文件中

这是我的代码,我需要保存外部文件wav或smth。

所以此时有记录按钮播放按钮和播放对象

  private void recordButton_Click(object sender, EventArgs e)
    {
        // Get audio data in 1/2 second chunks
        microphone.BufferDuration = TimeSpan.FromMilliseconds(500);

        // Allocate memory to hold the audio data
        buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];

        // Set the stream back to zero in case there is already something in it
        stream.SetLength(0);

        // Start recording
        microphone.Start();

        SetButtonStates(false, false, true);
        UserHelp.Text = "recording";
        // StatusImage.Source = microphoneImage;
    }

      private void playButton_Click(object sender, EventArgs e)
    {
        if (stream.Length > 0)
        {
            // Update the UI to reflect that
            // sound is playing
            SetButtonStates(false, false, true);
            UserHelp.Text = "play";

            // Play the audio in a new thread so the UI can update.
            Thread soundThread = new Thread(new ThreadStart(playSound));
            soundThread.Start();
        }
    }
    private void playSound()
    {
        // Play audio using SoundEffectInstance so we can monitor it's State 
        // and update the UI in the dt_Tick handler when it is done playing.
        SoundEffect sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
        soundInstance = sound.CreateInstance();
        soundIsPlaying = true;
        soundInstance.Play();
    }

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您需要正确构建.wav标头,然后编写音频流。 查看this example