无法在 Windows Phone 8 中播放使用麦克风录制的 MP3 文件
本文关键字:麦克风 文件 MP3 播放 Windows Phone | 更新日期: 2023-09-27 18:27:29
/// <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播放器播放。但是当我使用设备时,我无法打开文件。有没有解决方案
来自WP8麦克风的音频数据不是MP3,而是PCM波形格式:
相关问题
我建议不要将其编码为 MP3,而是使用 WMA,这是内置 API 直接支持的另一种格式。 此问题包含您需要的示例:
将麦克风数据保存到音频文件