2015-06-18 141 views
1

我嘗試使保存在內存流中的一些緩衝區連接在一起。然後,當我試圖打全緩衝它提供了一個例外:Stream.CopyTo(newStream)返回長度0

類型「System.ArgumentException」發生在 Microsoft.Xna.Framework.ni.dll但在用戶沒有處理的例外代碼

附加信息:確保緩衝區長度非零並且 滿足音頻格式的塊對齊要求。

當我調試mStrm仍然是0時,找不到原因。

private void mySendClick(object sender, RoutedEventArgs e) 
    { 
     var mStrmStartDelimiter = new MemoryStream(); 
     var mStrmEndDelimiter = new MemoryStream();    
     BinaryWriter writer1 = new BinaryWriter(mStrmStartDelimiter); 
     Sinus(6500, 200, writer1, 32767); 
     BinaryWriter writer2 = new BinaryWriter(mStrmEndDelimiter); 
     Sinus(6800, 200, writer2, 32767);   
     var mStrm = new MemoryStream(); 

     mStrmStartDelimiter.CopyTo(mStrm); 

      //ToDO 
     mStrmEndDelimiter.CopyTo(mStrm); 
     mStrm.Seek(0, SeekOrigin.Begin); 

     SoundEffect mySoundPlay = new SoundEffect(mStrm.ToArray(), 16000, AudioChannels.Mono); 
     mySoundPlay.Play(); 

    } 

    public static void Sinus(double frequency, int msDuration, BinaryWriter writer, int volume) 
    { 
     double TAU = 2 * Math.PI; 
     double samplesPerSecond = 16000; 
     double theta = frequency * TAU/(double)samplesPerSecond; 
     int samples = (int)((decimal)samplesPerSecond * msDuration/1000); 
     // 'volume' is UInt16 with range 0 thru Uint16.MaxValue (= 65 535) 
     // we need 'amp' to have the range of 0 thru Int16.MaxValue (= 32 767) 
     double amp = volume >> 2; // so we simply set amp = volume/2 
     for (int step = 0; step < samples; step++) 
     { 
      short s = (short)(amp * Math.Sin(theta * (double)step)); 
      writer.Write(s); 
     } 
    } 

我針對的Windows Phone 8.1的Silverlight平臺

回答

0

我對問題的解決方案:請執行下列操作調用CopyTo從()

mStrmStartDelimiter.Position = 0; 
mStrmEndDelimiter.Position = 0;