Search code examples
c#silverlightwindows-phone-7streammediaelement

MediaElement's SetSource using a custom stream inheriting from IsolatedStorageFileStream


I have a class called XorIsoStoreFileStream inheriting from IsolatedStorageFileStream, and the point is that using this class, stuff is written with an XOR "encryption" and it can also be read using this class when it XORs it back. For example:

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;
}

This seems to be working okay everywhere else in the program. Now I need to play an mp3 file from there, and because of the overridden Read and ReadByte, it should work just as if I'd given SetSource an IsolatedStorageFileStream. It will not take my Xor class though. When I hit play, it hits a NotSupportedException at the SetSource line saying, "Stream must be of type 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?
        }
    }
}

Is there something else I can override, like SetSource itself? Doesn't seem like that'd help.
Do I have to implement MediaStreamSource? That seems like huge overkill, reinventing the wheel, etc.
Or is this just not going to work? Will I have to save decrypted parts of the file to a temporary place and SetSource to a normal IsolatedStorageFileStream?


Solution

  • You could use MediaStreamSource, but that will be quite time-consuming. It would be much easier in your case to process an instance of IsolatedStorageFileStream right before playback and passing it to the MediaElement instead of having overrides and passing a custom class.

    You will have problems passing custom classes, since you need to use a native IsolatedStorageFileStream to pass it through SetSource, as it is stated in the official doc.

    Passing a generic stream to SetSource(System.IO.Stream) is not supported in Silverlight for Windows Phone. However, the IsolatedStorageFileStream class, which derives from Stream, is suppoted on Silverlight for Windows Phone.