我正在辛苦地尝试为我的这个问题提出一个好的解决方案。
我有3种音效被标记为手机上的内容,并且都是相同的比特率。
sound1.wav
sound2.wav
sound3.wav
我希望用户能够以任何顺序(或他们喜欢的许多次)选择wav文件的播放顺序,然后根据他们的选择生成一个新的wav文件,该文件可以存储并从中恢复。隔离存储。
因此,到目前为止,Thank You Walt Ritscher
都有帮助,但是您最终将看到我的编码技能充其量是零散的。我在下面的代码中试图传达的是,将通过轻击事件提示用户选择任何/所有声音,而他的选择将确定新的音效听起来是什么样的(顺序等)。还有很多我不知道的地方,这是我想出的代码(不在我的编码计算机上);
//SO I have this master list of sounds to use, indicated in this block of code:
//sound1.wav, sound2.wav, sound3.wav
// my wav files are marked as resources
// get the wav file from the DLL
var streamInfo1 = Application.GetResourceStream(
new Uri(sound1.wav, UriKind.Relative));
var streamInfo2 = Application.GetResourceStream(
new Uri(sound2.wav, UriKind.Relative));
var streamInfo3 = Application.GetResourceStream(
new Uri(sound3.wav, UriKind.Relative));
//With the above declarations made, I run each one as a stream as a variable.
var stream1 = streamInfo1.Stream as UnmanagedMemoryStream;
var stream2 = streamInfo2.Stream as UnmanagedMemoryStream;
var stream3 = streamInfo3.Stream as UnmanagedMemoryStream;
//The user can choose the sounds in any order, and repeat if desired.
//Let us assume the user chose in this order:
//sound1.wav + sound1.wav + sound2.wav +sound3.wav
for (int i = 0; i < 10; i++)
{
var bytesA = new byte[stream1.Length];
var bytesB = new byte[stream1.Length];
var bytesC = new byte[stream2.Length];
}
// read the bytes from the stream into the array
stream1.Read(bytesA, 0, (int)stream1.Length);
stream2.Read(bytesB, 0, (int)stream1.Length);
stream3.Read(bytesC, 0, (int)stream2.Length);
var combined = new byte[bytesA.Length + bytesA.Length + bytesB.Length] + bytesC.Length]];
// copy the bytes into the combined array
System.Buffer.BlockCopy(bytesA, 0, combined, 0, bytesA.Length);
System.Buffer.BlockCopy(bytesB, 0, combined, bytesA.Length, bytesB.Length);
var outputStream = new MemoryStream();
outputStream.Write(combined, 0, combined.Length);
// substitute your own sample rate
var effect = new SoundEffect(
buffer: outputStream.ToArray(),
sampleRate: 48000,
channels: AudioChannels.Mono);
var instance = effect.CreateInstance();
instance.Play();
// save stream to IsolatedStorage
最佳答案
基本上,您必须从流中获取字节,然后合并为一个新的字节数组。然后将该数组存储到UnmanagedMemoryStream中。
// my wav files are marked as resources
// get the wav file from the DLL
var streamInfo1 = Application.GetResourceStream(
new Uri(loopWav, UriKind.Relative));
var streamInfo2 = Application.GetResourceStream(
new Uri(beatWav, UriKind.Relative));
var stream1 = streamInfo1.Stream as UnmanagedMemoryStream;
var stream2 = streamInfo2.Stream as UnmanagedMemoryStream;
var bytesA = new byte[stream1.Length];
var bytesB = new byte[stream2.Length];
// read the bytes from the stream into the array
stream1.Read(bytesA, 0, (int)stream1.Length);
stream2.Read(bytesB, 0, (int)stream2.Length);
var combined = new byte[bytesA.Length + bytesB.Length];
// copy the bytes into the combined array
System.Buffer.BlockCopy(bytesA, 0, combined, 0, bytesA.Length);
System.Buffer.BlockCopy(bytesB, 0, combined, bytesA.Length, bytesB.Length);
var outputStream = new MemoryStream();
outputStream.Write(combined, 0, combined.Length);
// substitute your own sample rate
var effect = new SoundEffect(
buffer: outputStream.ToArray(),
sampleRate: 48000,
channels: AudioChannels.Mono);
var instance = effect.CreateInstance();
instance.Play();
// save stream to IsolatedStorage
我已经为CoDe Magazine撰写了有关WP7音频的更多信息。
http://www.code-magazine.com/Article.aspx?quickid=1109071
关于c# - 串联SoundEffects(wav)并保存到隔离存储,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8142659/