2012-05-13 328 views
0

我正在为Windows 8设计一款录音机应用程序,并且注意到当我将IRandomAccessStream传递给MediaElement.SetSource时,应用程序崩溃,Visual Studio不会看到任何异常。我应该如何调试这个问题?什么可能导致它?MediaElement.SetSource崩溃应用程序

这是导致崩溃的代码:

void mediaFiles_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (e.AddedItems.Count == 1) 
    { 
     string fname = e.AddedItems[0] as string; 
     Stream fstream = GlobalVariables.encryptedFS.OpenFile(fname); 
     MediaElement elem = new MediaElement(); 
     mainGrid.Children.Add(elem); 
     elem.AutoPlay = true; 
     elem.SetSource(new WinRTStream(fstream, true), "audio/x-ms-wma"); 
    } 
} 

回答

0

固定它。原来是我的IAsyncOperationWithProgress实现中的一个错误。
对于那些面临类似的困难,下面是固定它的代码:

class ReadOperation : IAsyncOperationWithProgress<IBuffer, uint> 
{ 
    Stream _underlyingstream; 
    IAsyncAction _task; 
    IBuffer val; 
    byte[] _buffer; 
    int bytesread; 

    public ReadOperation(Stream str, IBuffer buffer, uint cnt) 
    { 
     uint count = cnt; 
     _underlyingstream = str; 

     if (_underlyingstream.Length - _underlyingstream.Position < count) 
     { 

      _buffer = new byte[_underlyingstream.Length - _underlyingstream.Position]; 
      count = (uint)_buffer.Length; 
     } 

     _buffer = new byte[count]; 
     val = buffer; 

     _task = Task.Run(async delegate() 
     { 
      while (bytesread < count) 
      { 
       int cout = await str.ReadAsync(_buffer, bytesread, (int)count); 
       if (cout == 0) 
       { 
        break; 
       } 
       bytesread += cout; 
      } 
     }).AsAsyncAction(); 
    } 
} 
+0

我要等待2天接受这一点。 – IDWMaster

+0

不知道这个问题是否存在,但你现在可以接受你的答案;) –