1
我開發的應用程序的一部分允許用戶使用麥克風錄製任何內容。要從麥克風錄音,我使用Microphone Code sample by Microsoft中給出的代碼。我只是對代碼進行了一些更改,以便將wav標頭寫入流,以便將記錄作爲波形文件存儲在獨立存儲中。Windows Phone中的麥克風和播放錄音
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);
//Write wav header
WriteWavHeader(microphone.SampleRate);
// Start recording
microphone.Start();
SetButtonStates(false, false, true,false);
UserHelp.Text = "record";
StatusImage.Source = microphoneImage;
StatusImage.Visibility = System.Windows.Visibility.Visible;
}
public void WriteWavHeader(int sampleRate)
{
const int bitsPerSample = 16;
const int bytesPerSample = bitsPerSample/8;
var encoding = System.Text.Encoding.UTF8;
// ChunkID Contains the letters "RIFF" in ASCII form (0x52494646 big-endian form).
stream.Write(encoding.GetBytes("RIFF"), 0, 4);
// NOTE this will be filled in later
stream.Write(BitConverter.GetBytes(0), 0, 4);
// Format Contains the letters "WAVE"(0x57415645 big-endian form).
stream.Write(encoding.GetBytes("WAVE"), 0, 4);
// Subchunk1ID Contains the letters "fmt " (0x666d7420 big-endian form).
stream.Write(encoding.GetBytes("fmt "), 0, 4);
// Subchunk1Size 16 for PCM. This is the size of therest of the Subchunk which follows this number.
stream.Write(BitConverter.GetBytes(16), 0, 4);
// AudioFormat PCM = 1 (i.e. Linear quantization) Values other than 1 indicate some form of compression.
stream.Write(BitConverter.GetBytes((short)1), 0, 2);
// NumChannels Mono = 1, Stereo = 2, etc.
stream.Write(BitConverter.GetBytes((short)1), 0, 2);
// SampleRate 8000, 44100, etc.
stream.Write(BitConverter.GetBytes(sampleRate), 0, 4);
// ByteRate = SampleRate * NumChannels * BitsPerSample/8
stream.Write(BitConverter.GetBytes(sampleRate * bytesPerSample), 0, 4);
// BlockAlign NumChannels * BitsPerSample/8 The number of bytes for one sample including all channels.
stream.Write(BitConverter.GetBytes((short)(bytesPerSample)), 0, 2);
// BitsPerSample 8 bits = 8, 16 bits = 16, etc.
stream.Write(BitConverter.GetBytes((short)(bitsPerSample)), 0, 2);
// Subchunk2ID Contains the letters "data" (0x64617461 big-endian form).
stream.Write(encoding.GetBytes("data"), 0, 4);
// NOTE to be filled in later
stream.Write(BitConverter.GetBytes(0), 0, 4);
}
public void UpdateWavHeader()
{
//if (!stream.CanSeek) throw new Exception("Can't seek stream to update wav header");
var oldPos = stream.Position;
// ChunkSize 36 + SubChunk2Size
stream.Seek(4, SeekOrigin.Begin);
stream.Write(BitConverter.GetBytes((int)stream.Length - 8), 0, 4);
// Subchunk2Size == NumSamples * NumChannels * BitsPerSample/8 This is the number of bytes in the data.
stream.Seek(40, SeekOrigin.Begin);
stream.Write(BitConverter.GetBytes((int)stream.Length - 44), 0, 4);
stream.Seek(oldPos, SeekOrigin.Begin);
}
private void stopButton_Click(object sender, EventArgs e)
{
if (microphone.State == MicrophoneState.Started)
{
// In RECORD mode, user clicked the
// stop button to end recording
microphone.Stop();
UpdateWavHeader();
}
else if (soundInstance.State == SoundState.Playing)
{
// In PLAY mode, user clicked the
// stop button to end playing back
soundInstance.Stop();
}
SetButtonStates(true, true, false,true);
UserHelp.Text = "ready";
StatusImage.Source = blankImage;
}
之後,如果用戶選擇保存錄制內容,我將它保存在獨立存儲器中。
應用程序中有各種頁面,使用麥克風的頁面就是其中之一。這個頁面做導航是從另一個頁面完成:
NavigationService.Navigate(new Uri("/Mic.xaml", UriKind.Relative));
現在,當Mic.xaml頁面上,有或沒有,如果用戶點擊硬件後退按鈕他回到舊的一頁記錄任何東西。在此頁面中,如果他再次導航到Mic.xaml頁面,然後嘗試錄製內容,則錄製的文件似乎無法正確錄製。錄音似乎有問題。
因此,當我們第一次導航到頁面時,錄製成功,但是當我們第二次或第四次導航時,它不會。
我在模擬器上
感謝一噸:) It wo像魅力一樣飛了過去 – Saurabh 2012-01-07 19:35:56