MediaElement的SetSource使用从IsolatedStorageFileStream继承的自定义流

时间:2011-08-01 22:48:04

标签: c# silverlight windows-phone-7 stream mediaelement

我有一个名为XorIsoStoreFileStream的类,它继承自IsolatedStorageFileStream,关键是使用这个类,stuff用XOR“加密”写入,当它将XOR返回时也可以使用这个类读取。例如:

public override int Read( byte[] buffer, int offset, int count )
{
    int startbyte = (int)base.Position;
    int readlength = base.Read( buffer, offset, count );
    xor( startbyte, buffer );

    return readlength;
}

这似乎在程序中的其他地方都能正常工作。现在我需要从那里播放一个mp3文件,并且由于被覆盖的Read和ReadByte,它应该像我给SetSource一个IsolatedStorageFileStream一样工作。虽然它不会占用我的Xor课程。当我点击播放时,它会在SetSource行遇到NotSupportedException,说“Stream必须是IsolatedStorageFileStream类型”。

using( var appStorage = IsolatedStorageFile.GetUserStoreForApplication() )
{
    if( appStorage.FileExists( path ) )
    {
        using( var stream = new XorIsoStoreFileStream( path, FileMode.Open, appStorage ) )
        {
            App.MainAudioPlayer.SetSource( stream );  // how to put Xor stream here?
        }
    }
}

还有其他我可以覆盖的东西,比如SetSource本身吗?似乎不是那样的帮助。
我必须实现MediaStreamSource吗?这似乎是巨大的矫枉过正,重新发明轮子等 或者这只是不起作用?我是否必须将文件的解密部分保存到临时位置,将SetSource保存到正常的IsolatedStorageFileStream?

1 个答案:

答案 0 :(得分:0)

您可以使用MediaStreamSource,但这会非常耗时。在您的情况下,在回放之前处理IsolatedStorageFileStream的实例并将其传递给MediaElement而不是覆盖并传递自定义类会更容易。

传递自定义类时会遇到问题,因为您需要使用原生IsolatedStorageFileStream来传递SetSource,因为它已声明为in the official doc

  

将通用流传递给SetSource(System.IO.Stream)不是   Silverlight for Windows Phone支持。然而   IsolatedStorageFileStream类,派生自Stream,是   支持Silverlight for Windows Phone。