无法在Windows Phone 8中播放使用麦克风录制的MP3文件

时间:2014-10-23 12:10:59

标签: c# windows-phone-8 microphone recording

    /// <summary>
    /// Updates the XNA FrameworkDispatcher and checks to see if a sound is playing.
    /// If sound has stopped playing, it updates the UI.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void dt_Tick(object sender, EventArgs e)
    {
        try { FrameworkDispatcher.Update(); }
        catch { }

        if (true == soundIsPlaying)
        {
            if (soundInstance.State != SoundState.Playing)
            {
                // Audio has finished playing
                soundIsPlaying = false;

                // Update the UI to reflect that the 
                // sound has stopped playing
                SetButtonStates(true, true, false);
                UserHelp.Text = "press play\nor record";
                StatusImage.Source = blankImage;
            }
        }
    }

    /// <summary>
    /// The Microphone.BufferReady event handler.
    /// Gets the audio data from the microphone and stores it in a buffer,
    /// then writes that buffer to a stream for later playback.
    /// Any action in this event handler should be quick!
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void microphone_BufferReady(object sender, EventArgs e)
    {
        // Retrieve audio data
        microphone.GetData(buffer);

        // Store the audio data in a stream
        stream.Write(buffer, 0, buffer.Length);

        var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();

        if (isoStore.FileExists("AudioTest.mp3"))
            isoStore.DeleteFile("AudioTest.mp3");
        using (var targetFile = isoStore.CreateFile("AudioTest.mp3"))
        {
            // WavHeaderWriter.WriteHeader(targetFile, (int)stream.Length, 1, microphone.SampleRate);
            var dataBuffer = stream.GetBuffer();

            targetFile.Write(dataBuffer, 0, (int)stream.Length);
            targetFile.Flush();
            targetFile.Close();
        }
    }

    /// <summary>
    /// Handles the Click event for the record button.
    /// Sets up the microphone and data buffers to collect audio data,
    /// then starts the microphone. Also, updates the UI.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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 = "record";
        StatusImage.Source = microphoneImage;
    }

我有这个代码用于使用麦克风录制并将其写入本地文件夹中的mp3文件。使用模拟器时,我可以访问该文件,并使用VLC播放器播放。但是当我使用设备时,我无法打开文件。有没有解决方案

1 个答案:

答案 0 :(得分:0)

来自WP8麦克风的音频数据不是MP3,它是PCM波形式:

Related Question

我建议您不要将其编码为MP3,而是使用WMA,这是内置API直接支持的另一种格式。这个问题有您需要的样本:

Save Microphone Data to Audio File