【问题标题】:Can't play MP3 File recorded using Microphone in windows phone 8无法在 windows phone 8 中播放使用麦克风录制的 MP3 文件
【发布时间】:2014-10-23 12:10:59
【问题描述】:
    /// <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 播放器播放。但是当我使用设备时,我无法打开文件。有什么解决办法

【问题讨论】:

  • stream 是什么?除了文件名之外,我在这里看不到任何与 mp3 相关的内容。我们应该如何提供帮助?
  • 仅将 .mp3 放在文件末尾并不能使其成为 mp3。 MP3 是编码音频数据的特定方式。您需要弄清楚麦克风数据的格式,然后将其编码为您想要的格式

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


【解决方案1】:

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

Related Question

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

Save Microphone Data to Audio File

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多