0

我正在开发适用于 Windows 8 的录音机应用程序,并注意到当我将 IRandomAccessStream 传递给 MediaElement.SetSource 时,应用程序崩溃,并且没有抛出 Visual Studio 可见的异常。我应该如何调试这个问题?可能是什么原因造成的?

这是导致崩溃的代码:

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");
    }
}
4

1 回答 1

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();
    }
}
于 2012-05-13T18:10:15.593 回答