我目前正在Windows Phone 8应用程序中将音频流保存为.wav文件。将流保存为.aac文件需要进行哪些更改?
public void saveAudioBuffer()
{
IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();
//create a random file name and then assign
Random rand = new Random(DateTime.Now.Millisecond);
int rvalue = rand.Next(1000000000, 2000000000);
newAudioFileName = "NEW_AUDIO_" + rvalue + "_FILE_NAME.wav";
string fileName = ConfigurationInfo.savedFilesDirectory + newAudioFileName;
if(myStore.FileExists(fileName))
myStore.DeleteFile(fileName);
try
{
using (var isoFileStream = new IsolatedStorageFileStream(
fileName,
FileMode.OpenOrCreate,
myStore))
{
// Write a header before the actual pcm data
int sampleBits = 16;
int sampleBytes = sampleBits / 8;
int byteRate = microphone.SampleRate * sampleBytes * 1;
int blockAlign = sampleBytes * 1;
Encoding encoding = Encoding.UTF8;
isoFileStream.Write(encoding.GetBytes("RIFF"), 0, 4); // "RIFF"
isoFileStream.Write(BitConverter.GetBytes(0), 0, 4); // Chunk Size
isoFileStream.Write(encoding.GetBytes("WAVE"), 0, 4); // Format - "Wave"
isoFileStream.Write(encoding.GetBytes("fmt "), 0, 4); // sub chunk - "fmt"
isoFileStream.Write(BitConverter.GetBytes(16), 0, 4); // sub chunk size
isoFileStream.Write(BitConverter.GetBytes((short)1), 0, 2); // audio format
isoFileStream.Write(BitConverter.GetBytes((short)1), 0, 2); // num of channels
isoFileStream.Write(BitConverter.GetBytes(microphone.SampleRate), 0, 4); // sample rate
isoFileStream.Write(BitConverter.GetBytes(byteRate), 0, 4); // byte rate
isoFileStream.Write(BitConverter.GetBytes((short)(blockAlign)), 0, 2); // block align
isoFileStream.Write(BitConverter.GetBytes((short)(sampleBits)), 0, 2); // bits per sample
isoFileStream.Write(encoding.GetBytes("data"), 0, 4); // sub chunk - "data"
isoFileStream.Write(BitConverter.GetBytes(0), 0, 4); // sub chunk size
// write the actual pcm data
stream.Position = 0;
stream.CopyTo(isoFileStream);
// and fill in the blanks
long previousPos = isoFileStream.Position;
isoFileStream.Seek(4, SeekOrigin.Begin);
isoFileStream.Write(BitConverter.GetBytes((int)isoFileStream.Length - 8), 0, 4);
isoFileStream.Seek(40, SeekOrigin.Begin);
isoFileStream.Write(BitConverter.GetBytes((int)isoFileStream.Length - 44), 0, 4);
isoFileStream.Seek(previousPos, SeekOrigin.Begin);
isoFileStream.Flush();
}
}
catch
{
MessageBox.Show("Error while trying to store audio stream.");
}
}
在我的应用程序中,我使用了Microsoft.Xna.Framework.Audio中的Microphone对象来捕获声音。