简体   繁体   English

如何在后面的代码中设置视频标签的来源?

[英]How to set source of video tag in code behind?

I use this code in ASPX file: 我在ASPX文件中使用以下代码:

 <video width="320" height="240" autoplay="autoplay">
    <source id="videoSrc" runat="server"  type="video/mp4"/>
    Your browser does not support the video tag.
 </video>

but when I use this code in code behind: 但是当我在后面的代码中使用此代码时:

protected void Page_Load(object sender, EventArgs e)
{
    videoSrc.Src= "UploadMovies/"+Request.QueryString["id"]+"/high.mp4";
}

and call my page as myPage.aspx?id=1 I get this error on <source> : 并将我的页面称为myPage.aspx?id=1我在<source>上收到此错误:

The base class includes the field 'videoSrc', but its type (System.Web.UI.HtmlControls.HtmlSource) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl). 基类包括字段“ videoSrc”,但其类型(System.Web.UI.HtmlControls.HtmlSource)与控件的类型(System.Web.UI.HtmlControls.HtmlGenericControl)不兼容。

There are few things you could do here. 您在这里可以做的事情很少。

First is to get rid of <source> completely and use src attribute. 首先是完全摆脱<source>并使用src属性。 You need to make video a server-side control, but that won't cause the error: 您需要将video设为服务器端控件,但这不会导致错误:

<video width="320" height="240" autoplay="autoplay" id="video" runat="server">
</video>

video.Attributes["src"] = "UploadMovies/"+Request.QueryString["id"]+"/high.mp4";

Another thing is to have a code behind function that will give you a video link: 另一件事是在函数后面有一个代码,该代码将为您提供视频链接:

<video width="320" height="240" autoplay="autoplay">
    <source type="video/mp4" src='<%= GetVideoLink() %>'/>
</video>

protected string GetVideoLink()
{
    return "UploadMovies/"+Request.QueryString["id"]+"/high.mp4";
}

Here you can also use parameters and have several <source> tags to support fallback. 在这里,您还可以使用参数并具有多个<source>标记以支持回退。

As to the error you are seeing, it is not obvious why would that be happening. 至于您所看到的错误,尚不清楚为什么会发生。 HtmlSource is the right type of control for source tag, it is not clear why ASP.NET decides to treat it as generic html control instead. HtmlSource是用于source标记的控件的正确类型,尚不清楚为什么ASP.NET决定将其视为通用html控件。 You can try this workaround though. 您可以尝试这种解决方法

Not specifying the source attribute may lead to incompatibility issues on some browsers (eg: Safari. See https://github.com/mediaelement/mediaelement/issues/486 ). 不指定source属性可能导致某些浏览器不兼容(例如Safari。请参阅https://github.com/mediaelement/mediaelement/issues/486 )。

Not a big deal, though. 不过没什么大不了的。 Source inner tags can be created from server side: 可以从服务器端创建Source内部标签:

// Assuming we have runat="server" video tag in the markup side, with ID=vid:
// We could cast it as HtmlGenericControl. e.g: in a ItemDataBound event of a Repeater

// Now create the source tag    
HtmlGenericControl source1 = new HtmlGenericControl("source");

source1.Attributes["src"] = "your_video_url";
source1.Attributes["type"] = "video/mp4";

// We can also add additional sources:
HtmlGenericControl source2 = new HtmlGenericControl("source");
source2.Attributes["src"] = "your_video_second_url";
source2.Attributes["type"] = "video/webm";

// Now add the sources as child controls of the video control
vid.Controls.Add(source1);
vid.Controls.Add(source2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM