3.22.2026

Cách nghe nhạc YouTube theo trang Hoctroviet

Bạn,

Theo lời yêu cầu của bạn HT, tôi "bật mí" cho các bạn (lần nữa) cách chèn mã vào trong trang nhà của bạn để bạn có thể nghe nhiều nhạc mà không cần phải chèn (embed) nhiều windows.

Lưu ý: Sáng nay (27 tháng 4) do hơi vội nên tôi chép thiếu hai dòng (chỗ chữ xanh bước 2), nếu bạn nào lỡ thử dùng mà không được cho tôi gửi lời xin lỗi. Tôi đã sửa lại và thử dùng mã mới thêm vào và lắp thành công trang này. Thời gian lắp ít hơn 5 phút nếu bạn từ từ làm, còn tôi vì biết chỗ nào lắp vào rồi thì chỉ tốn dưới 1 phút là xong. (Hoctro)

Bước 1: Mở trang Design, vào Edit HTML rồi chèn đoạn mã này ngay ở đầu:






<!-- Bước 1: bắt đầu chèn mã từ chỗ này -->
<script src='http://www.google.com/jsapi' type='text/javascript'/>
<link href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5//themes/base/jquery.ui.all.css' rel='stylesheet'/>

<!-- Đây là CSS để sửa vị trí và các chiều ngang và dọc của youtube -->
<style>
#videoDiv { margin-right: 3px;}
#videoInfo {margin-left: 3px;}
#content-links {RIGHT: 5px; WIDTH: 480px; height: 380px; POSITION: fixed; BOTTOM: 6px}
</style>

<!-- Đây là các lệnh gọi các fuctions của các libraries chính -->
<script src='http://www.google.com/jsapi' type='text/javascript'/>
<script type='text/javascript'>
google.load(&quot;swfobject&quot;, &quot;2.1&quot;);
</script>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js' type='text/javascript'/>
<script src='http://www.google.com/jsapi' type='text/javascript'/>


<!-- Đây là youtube code -->
<script type='text/javascript'>//<![CDATA[

var urls = [];
var incr = 0;
var total = "";
var titles = [];
var skip = 0;

function getParams() {
  $('div.post-body a[onclick]').each(function(i) {
    urls[i] = $(this).attr('onclick');
    titles[i] = $(this).html();
    skip = 0;
  }); // each

  // alert(total);
}
// Callback main function
$(document).ready(function() {
  getParams() ;

  $('div.post-body a[onclick]').click(function() {
    var $element = $(this);
    updateHTML('videoHeader', $element.html());
  });
}); // function

//]]>
</script>


<script type='text/javascript'>
/*
* Change out the video that is playing
*/



// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
document.getElementById(elmId).innerHTML = value;
}

// Make the Youtube player visible or not. Basically at loading time it
// should be off, and only be on whenever the first video is clicked.
function ytVisible(value) {
attr = (value == true) ? &quot;visible&quot; : &quot;hidden&quot;;
document.getElementById(&quot;ytPlayer&quot;).style.visibility = attr;
document.getElementById(&quot;content-links&quot;).style.visibility = attr;
}

 // Loads the selected video into the player.

function loadVideo(id, num) {

  if(ytplayer) {
    ytVisible(true);
    if (!num) num=0;
    ytplayer.loadVideoById(id, num);
    //updateHTML(&#39;videoHeader&#39;, &quot;&quot;);
  }
  else // Flash not allowed, call loadVideoWindow
  {
    loadVideoWindow(id);
  }
}

// Open a new window in case the video doesn&#39;t allow embedding
// or for peaceful degradation purposes
function loadVideoWindow(id) {
  var ytlink;
  var newwindow;
  ytlink = &quot;http://www.youtube.com/watch?v=&quot; + id;
  newwindow=window.open(ytlink,&#39;name&#39;,&#39;height=600,width=700&#39;);
  if (window.focus) {newwindow.focus()}
  }
// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
  skip++;
  if (skip &lt; 5) genRandom();
 // alert(&quot;An error occured of type:&quot; + errorCode);
}

function genRandom() {
  var randomNumber=Math.floor(Math.random()*(urls.length-1));
  urls[randomNumber].apply();
  updateHTML(&#39;videoHeader&#39;, titles[randomNumber]);
}
// This function is called when an error is thrown by the player
function onStateChange(errorCode) {
 if (errorCode == 0) { // current vid is ended
   // Best line ever!!!
   genRandom();
  }
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById(&quot;ytPlayer&quot;);
ytplayer.addEventListener(&quot;onError&quot;, &quot;onPlayerError&quot;);
ytplayer.addEventListener(&quot;onStateChange&quot;, &quot;onStateChange&quot;);
ytVisible(false);

}


// The &quot;main method&quot; of this sample. Called when someone clicks &quot;Run&quot;.
function loadPlayer() {
// The video to load
var videoID = &quot;aym9D3xYzrs&quot;
// Lets Flash from another domain call JavaScript
var params = { allowScriptAccess: &quot;always&quot; };
// The element id of the Flash embed
var atts = { id: &quot;ytPlayer&quot; };
// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
swfobject.embedSWF(&quot;http://www.youtube.com/v/&quot; + videoID +
&quot;&amp;enablejsapi=1&amp;border=1&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;playerapiid=player1&quot;, &quot;videoDiv&quot;, &quot;480&quot;, &quot;350&quot;, &quot;8&quot;, null, null, params, atts);

}
function _run() {
loadPlayer();

}
google.setOnLoadCallback(_run);


</script>

<!-- Hết bước 1 -->


Tiếp theo, thêm vào bước 2 ở gần cuối khung, ngay trước trang tag /body:





<!-- Bước 2: Chèn một table chứa mã để hiển thị video -->
<div id='content-links'>
<div style='background-color:#000;'>

<table align='center' width='90%'>
<tr>
<td align='left' width='120%'>
<b><span id='videoHeader' style='color:#fff;'>...</span></b>   
</td>
<td align='right' width='20%'><a href='javascript:void(0);' onclick='genRandom();' style='color:#fff;'>  Skip</a></td>
</tr>
</table>
</div>
<div id='videoDiv'>Loading... </div>
</div>

<!-- Hết bước 2 - Nhớ chèn toàn bộ bước này trước tag /body -->



Bước 3: Cách tạo các links chứa bài:

Quy tắc #1: các bài phải nằm trong một div có class="post-body" mới chạy liên tục được. Nếu các nhãn nằm trong thân bài thì khỏi cần làm điều này vì thân bài đã là class="post-body" rồi.

Thí dụ như các links sau là thân bài, nên khỏi cần chèn (hình lấy từ trang nhạc Paul Mauriat)


còn các links sau phải chèn div với class="post-body" vì là một widget ngoài phần "body". Hình lấy từ widget "Raymond Lefevre" tôi mới lắp thêm.



<div class="post-body" >
<ul>
<li><a href="javascript:void(0);" onclick="loadVideo('9bGyrIciQHk');">VIENS VIENS </a></li>

<li><a href="javascript:void(0);" onclick="loadVideo('Ni2UKwfsZEQ');">TU TE RECONNAITRAS </a></li>

<li><a href="javascript:void(0);" onclick="loadVideo('NUXkAnEq7VI');">MY LOVE </a></li>

<li><a href="javascript:void(0);" onclick="loadVideo('D4Exp08zZXQ');">UN GRANDE AMORE E NIENTE PIU </a></li>

<li><a href="javascript:void(0);" onclick="loadVideo('J3SJ0RwXTqM');">REVIENS MON AMOUR REVIENS </a></li>

<li><a href="javascript:void(0);" onclick="loadVideo('WmhOn2RXys0');">NOUS IRONS A VERONE </a></li>

<li><a href="javascript:void(0);" onclick="loadVideo('sxI7jrqTrso')">Ainsi Soit-Il (1977)</a></li>
</ul>
</div>



Quy tắc #2: tạo các links.

Trong trang của mình, thêm vào tag "a" với href có cái ID của bài nhạc mình muốn nghe, thí dụ như:


<li><a href="javascript:void(0);" onclick="loadVideo('lOMhkGZdujY');">VOUS LES FEMMES (You the Women) (02:02)(Enrique Macias / P.-R. Blanc & Jacques Demarny)</a></li>


<li><a href="javascript:void(0);" onclick="loadVideo('-JutiwteRj8');">N'AVOUE JAMAIS (I Wanna Know) (02:01)(Guy Mardel - Francoise Dorin)</a></li>

Ngoài ra, bạn còn có thể bắt đầu bài nhạc từ số giây, thí dụ như bài Dancing on the ceiling dưới đây tôi cho bắt đầu từ 20 giây bằng cách them vào:

loadVideo('OdQDXs75Ulo',20);

và nếu bài nhạc không cho embed như bài Beat It, tôi cũng có thể mở một window để xem :-) Bạn phải dùng một function mới là loadVideoWindow, (thay vì chỉ là loadVideo)

loadVideoWindow('Uqxo1SKB0z8');

Hãy xem các biến thể đó ở đoạn sau:

"In 1985, a team of superstars gathered together to record the song"We Are the World". The song itself was a collaboration between Michael Jackson ((Beat It) and Lionel Richie (Hello, Dancing on the ceiling, etc.)"

Chúc bạn thành công, có gì thì hỏi ngay ở trang này.

Thân mến,

Hoctro

2 comments:

  1. Cám ơn anh rất nhiều. Hôm nay HT có một chút thời gian rảnh vào thăm Hoctroviet thấy có phần bài viết anh dành cho HT. Lúc đầu nhìn thấy hoa hết cả mắt, bình tĩnh một chút, mò mò rồi cũng ra. Phần hướng dẫn của anh rất rõ ràng, một người "mù" IT như HT mà cũng làm được thì sẽ có rất nhiều bạn nhờ bài viết này có thể thư giãn nhiều hơn với góc riêng của mình. Một lần nữa cảm ơn anh và chúc anh luôn vui khỏe. :)

    ReplyDelete
  2. Rất vui vì HT đã lắp đặt thành công!

    Chúc vui.

    ReplyDelete

Note: Only a member of this blog may post a comment.

Dear readers,

The following is a pinned post. Hoctro's Place (Góc Học Trò) is a place for me to deliver my past, present, and future thoughts about music and about my "vibe-coding" experiences with Claude Code, tips and tricks, so to speak. It's also a place to post my collaboration with Claude Code, ranging from supervising it to write analysis essays about prominent Vietnamese musicians such as Phạm Duy and Trịnh Công Sơn, to everything else that I find interesting.

For me, Claude AI's analysis essays are so in-depth and showing many new perspectives, it would be wasteful not to share with the world. It is a collaboration, because just like "vibe-coding", I might have not written the words, but I was the one whom conceived the original ideas, supplied the documents for Claude to research from, read and corrected hallucinations, and gave final approval for it to be published.

I sometimes print transcripts of interesting videos from other places, in order to share with others whom are more comfortable in reading and thinking things through. I don't have adsense as a side source income, so again if anything it's just helping the original video owners to gain more potential viewers, and readers to have readable material to learn.


Bạn đọc thân ái,

Sau đây là vài dòng tự sự. Hoctro's Place (Góc Học Trò) là chỗ để tôi chia sẻ những suy nghĩ của mình về âm nhạc và về những trải nghiệm "vibe-coding" với Claude Code, mấy mẹo hay ho mà tôi học được. Đây cũng là chỗ để tôi đăng những bài làm chung với Claude Code, từ việc tôi hướng dẫn nó viết bài phân tích về những nhạc sĩ Việt Nam nổi tiếng như Phạm Duy và Trịnh Công Sơn, cho tới đủ thứ khác mà tôi thấy hay.

Với tôi, những bài phân tích của Claude AI rất sâu sắc, chỉ ra nhiều góc nhìn mới, không chia sẻ với bạn đọc thì rất uổng phí. Nói là làm chung, bởi vì giống như "vibe-coding" vậy đó, tôi có thể không phải là người viết ra từng chữ, nhưng tôi là người nghĩ ra ý tưởng ban đầu, cung cấp tài liệu cho Claude nghiên cứu, đọc lại rồi sửa mấy chỗ nó viết sai, và quyết định cuối cùng có đăng hay không.

Thỉnh thoảng tôi cũng in lại nội dung mấy video hay từ chỗ khác, để chia sẻ cho những bạn nào thích đọc và suy ngẫm hơn là coi video. Tôi không có chạy quảng cáo kiếm tiền gì hết, nên nếu có gì thì cũng chỉ là giúp mấy chủ video gốc có thêm người xem, và giúp bạn đọc có thêm tài liệu để học thôi. Chào bạn và mong bạn tìm thấy những khoảnh khắc vui khi đọc trang này.