You Are Here: Home » Ajax » HMTL 5 video player con lista video da feeds di YouTube

HMTL 5 video player con lista video da feeds di YouTube

HMTL 5 video player con lista video da feeds di YouTube

Ho creato un video player utilizzando l’iframe che mette a disposizione YouTube con la classe youtube-player.

Questo video player č in HTML 5 e Javascript ( jQuery ). Carica gli standard feeds da YouTube e visualizza una lista dei video in base ad alcuni criteri: Most Viewed, Top Rated, Recently Featured, Most Discussed, Top Favorites, Most Responded e Most Recent.

Molto utile per imparare a lavorare con Javascript, HTML 5 e il player video di YouTube.

HTML 5

<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Droid+Sans" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="js/main.js" type="text/javascript" charset="utf-8"></script>

<title>JQuery YouTube feeds</title>
</head>

<body onLoad="init()">
    
    <section>
        <header>
            <h3>JQuery YouTube feeds | example by FlepStudio</h3>
        </header>
    
        <div id="wrapper">
            <div id="leftcolumn">
                
            </div>
            
            <div id="rightcolumn">Standard feeds
                <form name"myForm" method="GET" action="">
                    <select name="myList" onchange="checkResult(this.form)" id="combobox">
                        
                    </select>
                </form>
                
            <div id='mycustomscroll'>
                
            </div>
            
            </div>
        </div>
    
        <footer>
            
        </footer>
    </section>

</body>

</html>

CSS

body
{
    font-family: 'Droid Sans', 'Arial', sans-serif;
    color: #6F7986;
    font-size: 14px;
}

header
{
    text-align: center;
}

footer
{
    
}

select
{
    width: 240px;
}

#wrapper 
{ 
    margin: 0 auto;
    width: 960px;
}

#leftcolumn 
{ 
    color: #333;
    border: 1px solid #ccc;
    background-color: #F2F2E6;
    margin: 10px 5px 10px 0px;
    padding: 10px;
    height: 385px;
    width: 640px;
    float: left;
}

#rightcolumn 
{
    display: inline;
    position: relative;
    float: right;
    color: #666;
    border: 1px solid #ccc;
    background-color: #F2F2E6;
    margin: 10px 0px 10px 0px;
    padding: 10px;
    height: 385px;
    width: 265px;
}

#mycustomscroll 
{
    list-style: none;
    height: 325px;
    overflow: auto;
    position: relative;
    background-color: #F2F2E6;
    margin: 0.3em auto;
    
    font-size: 12px;
}

#mycustomscroll li
{
    font-size: 12px;
    width: 230px;
    background-color: #333;
    color: #CCC;
    padding: 5px;
    margin: 1px;
    cursor: pointer;
}

#mycustomscroll a
{
    text-decoration: none;
}

#mycustomscroll a:hover li
{
    background-color: #6F7986;
    color: #FFF;
}

Javascript e jQuery

// properties
var URL_YOUTUBE_FEEDS="http://gdata.youtube.com/feeds/api/standardfeeds/";
var labels=new Array("Most Viewed", "Top Rated", "Recently Featured", "Most Discussed", "Top Favorites", "Most Responded", "Most Recent");
var values=new Array("most_viewed", "top_rated", "recently_featured", "most_discussed", "top_favorites", "most_responded", "most_recent");
var videos_id=new Array();

// init the application
function init()
{
    // populate combobox
    for( var i = 0; i < labels.length; i++ )
    {
        $("#combobox").append('<option value="'+values[i]+'">'+labels[i]+'</option>');
    }
    
    // loadFeed call
    loadFeed(values[0]);
}


// check selected item
function checkResult(form)
{
    // empty left column
    $("#leftcolumn").empty();
    
    // get id and value of selected item
    var id = form.myList.selectedIndex;
    var result = form.myList.options[id].value;
    
    //loadFeed call
    loadFeed(result);
}


// load YouTube feed
function loadFeed(identifier)
{
    // new empty array
    videos_id=new Array();
    
    // empty mycustomscroll and left column
    $("#mycustomscroll").empty();
    $("#leftcolumn").empty();
    
    // use ajax to load the feed
    $.ajax({
        type: "GET",
        url: "php/proxy.php?url="+URL_YOUTUBE_FEEDS+identifier,
        dataType: "xml",
        
        // success function
        success: function(xml) 
        {
            // append <ul>
            $("#mycustomscroll").append('<ul>');
            
            // loop for each entry
            $(xml).find('entry').each(function looping(value)
            {    
                // get video id
                var id=$(this).find("link").attr("href");
                var video_id = id.split('v=')[1];
                var ampersandPosition = video_id.indexOf('&');
                if(ampersandPosition != -1)
                    video_id = video_id.substring(0, ampersandPosition);
                // push id into array
                videos_id.push(video_id);
                
                // append list of items
                $("#mycustomscroll").append('<a><li id="item"><strong>'+(value+1)+"</strong><br/><strong>Title: </strong>"+$(this).find("title").text()+"<br/><strong>Published: </strong>"+$(this).find("published").text()+"<br/><strong>Author: </strong>"+$(this).find("author").find("name").text()+'</li></a>');
            });
            
            // append </ul>
            $("#mycustomscroll").append('</ul>');
            // append iframe video player
            $("#leftcolumn").append('<iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/'+videos_id[0]+'" frameborder="0"></iframe>');
            
            addListeners();
        }
    });
}


// add <li> listener
function addListeners()
{
    // loop for each list item
    $('#mycustomscroll li').each(function looping(index) 
    {
        // onclick...
        $(this).click(function onItemClick() 
        {
            // empty left column
            $("#leftcolumn").empty();
            // append iframe video player
            $("#leftcolumn").append('<iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/'+videos_id[index]+'" frameborder="0"></iframe>');
        });
    });
}

DEMO

Files sorgente:

  Youtube feeds and video player with video list (6.6 KiB, 504 hits)

Sponsors of this article

About The Author

WebMaster

CEO e amministratore Network Flepstudio.org

Number of Entries : 85

Comments (2)

Leave a Comment

© 2012 Powered By Flepstudio.org

Scroll to top