我需要一个带有播放按钮的youtube缩略图代码,我点击播放视频后会从网址获取视频ID,弹出播放...有人可以建议......?
.aspx代码:
<asp:DataList ID="DataList3" runat="server" RepeatDirection="Horizontal"
RepeatColumns="7" Width="600px" DataSourceID="SqlDataSource1">
<ItemTemplate>
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
<object width="200" height="200"><param name="movie" value='<%#DataBinder.Eval(Container.DataItem, "url") %>'></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src='<%#DataBinder.Eval(Container.DataItem, "url") %>' type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="200" height="200">
</embed>
</object>
<br />
<br />
</ItemTemplate>
</asp:DataList>
.aspx.cs代码
string url = txturl.Text;
if (url.Contains("youtube.com"))
{
string ytFormattedUrl = GetYouTubeID(url);
if (!CheckDuplicate(ytFormattedUrl))
{
SqlCommand cmd = new SqlCommand("INSERT INTO YoutubeVideo VALUES ('" + ytFormattedUrl + "','" + lbluser.Text + "','" + title.Text + "','" + DateTime.Today.Date + "','" + DateTime.Now.TimeOfDay + "')", con);
using (con)
{
con.Open();
int result = cmd.ExecuteNonQuery();
if (result != -1)
{
DataList1.DataBind();
}
else { Response.Write("Error inserting new url!"); }
}
}
else { Response.Write("This video already exists in our database!"); }
}
else
{
Response.Write("This URL is not a valid YOUTUBE video link because it does not contain youtube.com in it");
}
}
private string GetYouTubeID(string youTubeUrl)
{
//RegEx to Find YouTube ID
Match regexMatch = Regex.Match(youTubeUrl, "^[^v]+v=(.{11}).*",
RegexOptions.IgnoreCase);
if (regexMatch.Success)
{
return "http://www.youtube.com/v/" + regexMatch.Groups[1].Value +
"&hl=en&fs=1";
}
return youTubeUrl;
}
public bool CheckDuplicate(string youTubeUrl)
{
bool exists = false;
SqlCommand cmd = new SqlCommand(String.Format("select * from YoutubeVideo where url='{0}'", youTubeUrl), con);
using (con)
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
exists = (dr.HasRows) ? true : false;
}
return exists;
}</code>
来自ablove代码我在datalist中获取youtube视频但是当我点击它时它会以200px * 200px的速度播放。
答案 0 :(得分:0)
我会这样做。
就个人而言,我不会嵌入视频,因为这是问题所在!相反,嵌入视频或类似的图片,onclick然后在新窗口中加载适当的视频。
更新模板
<ItemTemplate>
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
<a href="wherever" javacriptCommandToOpenPopUpHere><img src="picture.jpg"></a>
<br /><br />
</ItemTemplate>
您可能还需要更新Insert命令,因为它可能允许SQL注入。