Hello,
How would I go about being able to use a URI of a binary class.
For example, I need to be able to give the URI for a song, but the song comes for a SQL server which sends it in a binary form, which is on the memory.

Thanks so much for any help.

Dani AI

Generated

You cannot turn an in‑memory blob into a file path. MediaElement.Source really wants a URI, so you have two clean options that build on what and discussed:

  • If you already have the bytes in memory, avoid a URI entirely and feed the stream to the player. In Silverlight 3+ you can do this with SetSource.
// bytes comes from SQL Server
var bytes = GetSongBytesFromDatabase(songId);
using (var ms = new MemoryStream(bytes))
{
    mediaElement.SetSource(ms);   // plays MP3/WMA/WMV containers supported by SL
}

That uses the supported API for in‑memory playback and keeps you off disk. See MediaElement.SetSource(Stream) for details and format caveats. MediaElement.SetSource(Stream) docs

  • If you specifically need a URI (your original requirement), expose the blob via an HTTP endpoint and point Source at it. A simple ASP.NET handler or MVC action can stream the bytes without writing a temp file:
// ASP.NET handler (simplified)
public class SongHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext ctx)
    {
        int id = int.Parse(ctx.Request["id"]);
        // get a stream and MIME type from SQL (e.g., audio/mpeg)
        using (Stream dbStream = GetSongStream(id, out string mime))
        {
            ctx.Response.ContentType = mime;
            ctx.Response.BufferOutput = false;
            dbStream.CopyTo(ctx.Response.OutputStream);
        }
    }
    public bool IsReusable => false;
}
// XAML/C#: mediaElement.Source = new Uri("/media/song?id=123", UriKind.Relative);

Notes:

Recommended Answers

All 9 Replies

You need to play it?!

Yes, under SilverLight, I have A MediaElement

<MediaElement source = "The memory"/>

OK, I just asked you because I expected you need just to play a song from your application without interacting with Media Player or if you need to play a song file without any information about the software user uses.

So, to convert any file to its binary representative

System.IO.FileStream fs = new System.IO.FileStream(@"FilePath", System.IO.FileMode.Open);
byte[] fileAsBytes = new byte[fs.Length];
fs.Read(fileAsBytes, 0, fileAsBytes.Length);
fs.Close();

To convert it back to stream

System.IO.StreamReader sr =  new System.IO.StreamReader(new System.IO.MemoryStream(fileAsBytes));

Thanks,
I kinda see what you are getting at. The issue is that I have:
MediaElement.Source = (a URI)

That is what I can't think of is how to pass basically a class (on memory value), as a file path.
Thanks for the help thus far.

Yes, but now I am wondering if I did something wrong. I have a SQL server which sends it as binary. The issue is when I have the data I never have the file location. I just have the binary value it returned.

For example:
I query the server for a song.
It returns the song as binary.
I set the source of the media element to the value returned.

That thirds step is my issue.
Thanks for the help.

So this has nothing to do with files? You have a song stored as binary in the SQL Server and want to use MediaPlayer to play the song, but media player is asking for a URI and you only have a memory buffer?

Write out a temporary file..

You have SQL Blob storage, effectively the same thing:
http://blogs.msdn.com/jnak/archive/2008/11/05/silverlight-mediaelement-playing-a-video-stored-in-windows-azure-blob-storage.aspx

I also gave the solution!!! Back to 2nd reply!

Thanks for your help, with what you gave me it was able to complete the MediaPlayer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.