使用带有CefSharp或CefGlue的JavaScript的本地或流式音频

Local or streamed audio using JavaScript with CefSharp or CefGlue

本文关键字:音频 JavaScript CefGlue CefSharp      更新时间:2023-09-26

我一直在玩CefSharp和CefGlue,并设法使CefSharp在C#应用程序中运行得很好。

使用CefSharp,我能够执行JavaScript并从内存加载HTML。例如,要执行一些JavaScript,可以这样做:

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

加载一些HTML:

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

我知道我可以用从某个web服务器加载的一些文件加载<audio>。有没有办法从本地文件或通过CefGlue/Sharp流加载音频?

更好的是,如何使用"动态"JavaScript(例如,使用上面的ExecuteScript(...))来实现这一点?

我已经想通了。它是通过模拟web服务器来完成的。困难似乎在于对所请求内容的解释,这似乎是通过解析所请求的URL来完成的。否则一切都很好。

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