locked
Setting MediaElement source to something in memory (not a file)?

    Question

  • Hi all!

    I've got a bunch of encrypted video files living in my app data folder - I'm trying to write something to allow me to read them in, decrypt them, and then play them with a MediaElement. My hope is that (for security's sake) I can keep the decrypted data in memory only - if they have to be written out to the disk, there's always the chance that even as temp files they may not get cleaned up properly and be accessible later in their decrypted form.

    HOWEVER, in doing a lot of research, it seems like MediaElement.SetSource only seems to work with streams derived from files (and not those in memory).

    To illustrate, this code does NOT work:

    InMemoryRandomAccessStream stream = await EncryptionManager.DecryptClip(clip);
    
    ClipViewer.SetSource(stream,"");

    But this does...

    InMemoryRandomAccessStream stream = await EncryptionManager.DecryptClip(clip);
    
    DataReader reader = new DataReader(stream.GetInputStreamAt(0));
    await reader.LoadAsync((uint) stream.Size);
    IBuffer buffer = reader.ReadBuffer((uint) stream.Size);
    
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("temp.mov",CreationCollisionOption.ReplaceExisting);
    await FileIO.WriteBufferAsync(file,buffer);
    
    IRandomAccessStream stream2 = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
    
    ClipViewer.SetSource(stream2,"");

    Is there some other way of achieving this? Or, as an alternative, is there a foolproof way of creating temp files on disk that are GUARANTEED to be wiped as soon as the app is closed (even on crash... I can write cleanup into the app suspension but it still leaves open the possibility...).

    Thanks in advance!

    Thursday, March 27, 2014 2:00 AM

Answers

  • I am sure media can be played from memory but don't have my machine with me to test it. For temporary files, use:

    ApplicationData.Current.TemporaryFolder 

    Even if you miss deleting content from this folder once, you can try later. Also, if the system will feel this folder has unnecessary baggage, the OS itself will empty the folder.


    If a post solves the problem you asked, please mark it as Answer.


    Thursday, March 27, 2014 11:31 AM