c# - 连接 SoundEffects (wav) 并保存到隔离存储

标签 c# silverlight windows-phone-7 arrays soundeffect

我很难找到一个好的解决方案来解决我的这个问题。

我在手机上标记为内容的 3 种音效,都具有相同的比特率。

  • sound1.wav
  • sound2.wav
  • sound3.wav

我希望用户能够以任何顺序或他们喜欢的任意次数选择 wav 文件的播放顺序,然后根据他们的选择生成一个新的 wav 文件,该文件可以存储和播放从隔离存储带回来。

感谢 Walt Ritscher 到目前为止的帮助,但您最终会看到我的编码技能充其量是支离 splinter 的。我在下面的代码中试图传达的是,系统将提示用户通过点击事件选择任何/所有声音,并且他的选择将决定新的音效听起来像什么(顺序等)但是还有很多我不知道的,这是我想出的代码(虽然不在我的编码计算机上);

//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 杂志撰写了更多关于 WP7 音频的文章。

http://www.code-magazine.com/Article.aspx?quickid=1109071

关于c# - 连接 SoundEffects (wav) 并保存到隔离存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8142659/

相关文章:

c# - Silverlight 是否能够更改本地用户计算机上的文件?

Silverlight SelectedIndex 未正确数据绑定(bind)

c# - 没有Etag如何设置设备的孪生属性?

c# - 在 Json.net 中序列化枚举列表

c# - Autofac XML 配置和属性是否覆盖或提供(如果不存在)?

silverlight - 具有子窗口和共享数据的Silverlight MVVM模式

windows-phone-7 - ListPicker FullMode 选中项颜色

windows-phone-7 - WP7页面背景图片

c# - 当使用MVVM + WP7 + LongListSelector选中对号时,如何调用服务器

C# MySQL 作为多行字符串