user610650
user610650

Reputation:

Local or streamed audio using JavaScript with CefSharp or CefGlue

I've been toying with CefSharp and CefGlue and managed to get CefSharp to work quite well in a C# application.

With CefSharp, I am able to execute JavaScript and load HTML from memory. For example, to execute some JavaScript, one can do this:

view.ExecuteScript("setInterval(function(){alert(\"Hello\")},3000);");

To load some HTML:

model.LoadHtml(string.Format("<html><body><a href='{0}'>CefSharp Home</a></body></html>", home_url));

I know I can load <audio> with some file loaded from some web server. Is there a way to load audio either from a local file or through a stream with CefGlue/Sharp?

Even better, how can this be done with "dynamic" JavaScript (for example by using ExecuteScript(...) above)?

Upvotes: 3

Views: 1661

Answers (1)

user610650
user610650

Reputation:

I've figured it out. It's done by impersonating a web server. The difficulty appears to be the interpretation of what is requested, which seems to be done by parsing the requested URL. Otherwise it all works great.

public bool OnBeforeResourceLoad(IWebBrowser browser, 
    IRequestResponse requestResponse)
{
    IRequest request = requestResponse.Request;

    if (request.Url.EndsWith(".gif")) {
        MemoryStream stream = new System.IO.MemoryStream();
        Properties.Resources.FooImage.Save(
            stream, System.Drawing.Imaging.ImageFormat.Bmp);
        requestResponse.RespondWith(stream, "image/gif");
    }
    else {

        Stream resourceStream = new MemoryStream(Encoding.UTF8.GetBytes(
            "<html><body><img src=\"foo.gif\" /></body></html>"));
        requestResponse.RespondWith(resourceStream, "text/html");
    }

    return false;
}

Upvotes: 1

Related Questions