2011-11-15 89 views
2

我正在試圖想出一個很好的解決方案來解決我的這個問題。連接SoundEffects(wav)並保存到獨立存儲

我有3個聲音效果被標記爲手機上的內容,都是相同的比特率。

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

我希望用戶能夠能夠以任意順序來選擇,或不過很多時候他們喜歡, wav文件的播放順序,然後根據他們的選擇生成一個新的wav文件,這些文件可以存儲並從隔離存儲中取回。

所以Thank You Walt Ritscher到目前爲止的幫助,但你最終會看到我的編碼技能是最好的支離破碎。我想在下面的代碼中傳達的是,用戶將被提示選擇具有輕擊事件的任何/所有聲音,並且他的選擇將決定新的soundeffect聽起來像什麼(它的順序等)然而,還有很多我不知道的,這裏是我提出的代碼(而不是在我的編碼計算機上);

//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 

回答

1

基本上你必須從流中獲取字節併合併成一個新的字節數組。然後將該數組存儲到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

+0

在試圖將其應用到我所需要的(並感謝您的精彩代碼示例),我應該繼續前進,給每個聲音文件了獨特的「VAR字節#」,並創建相應的獨特的「流#.read「行。我可以創建一個while(i = 0,i

+0

我會回答我自己的問題,只是爲了更好地瞭解代碼,並在解決問題時給予適當的評價並解決問題: ) –

+0

你可以修改你自己的問題,並在那裏添加視覺效果 –