0

I'm trying to embed a video on my page, depending on which one the user selects after being presented with a list. On my page I have:

       <div id="vidContent" style="text-align:left">
           <object width="550px" height="350px" >
                 <asp:Literal ID="ltlVideo" runat="server"></asp:Literal>
            </object>
       </div>

And in the code behind I have:

        Dim strVidPath As String = "http://www.youtube.com/v/" & strVidID
        ltlVideo.Text = "<embed src='" & strVidPath & "' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' height='350' width='470'></embed>
        phVideoBanner.Visible = True

..

which works ok...if the you have the "strVidID"

It only seems to display and play if you have the strVidPath = www.youtube.com/v/_O7iUiftbKU

but not play if strVidPath = www.youtube.com/watch?v=_O7iUiftbKU ....which seems to be the normal URL in the address bar when watching a youtube video.

I want the user to be able to add a video to the page and I was thinking it would be easier if the paste in the URL of the video but now it seems I'll have to get them to paste in the videoID instead as it only seems to play when I use www.youtube.com/v/_O7iUiftbKU

Anyone know why this is?

thegunner
  • 6,883
  • 30
  • 94
  • 143

2 Answers2

2

Rather than trying to parse a YouTube watch page URL and construct an embed code yourself, you can use the oEmbed service to do it for you.

If you need to get back legacy embed codes rather than iframe embed codes, you'd need to pass iframe=0 as one of the URL parameters to the oEmbed service, like: http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch%3Fv%3DbDOYN-6gdRE&format=json&iframe=0

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • How did I not know about this service? This will come in very handy for me ... thanks. I'll leave my answer below for documentation purposes, but this should be voted the correct one. – jlmcdonald Dec 12 '12 at 17:46
  • oEmbed is an industry standard, but YouTube's oEmbed support is unfortunately only documented in that blog post. It really does need to make an appearance in the official YouTube API documentation, but some internal process-related roadblocks have prevented that. – Jeff Posnick Dec 12 '12 at 18:10
1

The URL structure with the word "watch" in it is, as you point out, Youtube's public facing web page, which includes a lot more than the video ... it includes all the other content you see on the page as well. In essence, it's a pointer that resolves to an HTML page, and you can't have an HTML page as the source of an embed element.

The URL structure that is proper (i.e. the one that works) is not a pointer to an HTML page but a pointer that resolves directly to the player itself, and thus can serve as the source of an embed element.

Here's a link to a Stack Overflow question whose answer includes a C# code block that takes a regular YouTube URL (in any of its forms) as an input, does a regex match, and returns just the Youtube ID -- should be pretty simple to modify it for your needs ... thus you can still continue to have your users paste in the whole video URL:

C# regex to get video id from youtube and vimeo by url

Community
  • 1
  • 1
jlmcdonald
  • 13,408
  • 2
  • 54
  • 64