从url在asp.net网站上播放youtube视频

时间:2013-07-17 12:41:01

标签: c# asp.net vb.net youtube

我有功能代码只能从网址播放youtube视频(例如youtube.com/watch?v = ....)。我在这个article中使用过它。但它不适用于缩短网址(如youtu.be / ....)。两种方式都可行吗?

 Protected Sub SetVideoForPlay()
    'YouTube Video URL
    Dim youtubeUrl As String = "http://www.youtube.com/watch?feature=endscreen&v=gfedwjAOMZI&NR=1"

    Dim vCode As String = youtubeUrl.Substring(youtubeUrl.LastIndexOf("v=") + 2)
    If vCode.Contains("&") Then
        vCode = vCode.Substring(0, vCode.LastIndexOf("&"))
    End If

    Dim sHtml As String = "<object width='{0}' height='{1}' data='http://www.youtube.com/v/{2}&autoplay=0' codetype='application/x-shockwave-flash'>" & vbCr & vbLf & "  &ltparam name='movie' value='http://           www.youtube.com/v/{2}&autoplay=0'></param></object>"
    'define frame size
    Dim sWidth As String = "500px"
    Dim sHeight As String = "500px"

    'insert code to the Div
    divVideo.InnerHtml = [String].Format(sHtml, sWidth, sHeight, vCode)
End Sub

1 个答案:

答案 0 :(得分:1)

缩短的网址实际上是第三方重定向到YouTube视频。所以我认为您需要找出缩短的链接(youtu.be或其他)指向的URL,然后使用该URL(youtube.com上的URL)来显示视频。