0

Windows 8用のサウンドレコーダーアプリで作業していますが、IRandomAccessStreamをMediaElement.SetSourceに渡すと、アプリケーションがクラッシュし、VisualStudioに表示される例外がスローされないことに気付きました。この問題をどのようにデバッグする必要がありますか?何が原因でしょうか?

クラッシュの原因となっているコードは次のとおりです。

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 に答える