javascript - 如何使用 Javascript 将 VTT 文件读入数组和循环

标签 javascript jquery arrays

我有一个带有 JW Player 字幕的 VTT 文件,我正在尝试创建交互式文字记录。为此,我需要将 VTT 文件读入数组,然后与数据进行交互。

以下是 VTT 文件的片段:

1
00:00:00 --> 00:00:05
65 MEP we have twitter handles for both of us on screen as well so if you want

2
00:00:05 --> 00:00:08
to interact with those afterwards that's the best way to do so now been going to

3
00:00:08,051 --> 00:00:12,310
be talking about a topic that's extremely important topic in my mind and

到目前为止,这是我的 Javascript:

$.get('http://dev.sharepoint-videos.com/test.vtt', function(data) {
     
     // Read all captions into an array
     var items = data.split('\n\r');
     
     console.log(items);
    
     //Loop through all captions
     $.each(items, function( index, value ) {
      
      var item = items[index].split('\n');
      console.log(item);    

      });

         
});

这是我的 Console.log 返回的内容

0: "1
↵00:00:00 --> 00:00:05
↵65 MEP we have twitter handles for both of us on screen as well so if you want
"
1: "↵2
↵00:00:05 --> 00:00:08
↵to interact with those afterwards that's the best way to do so now been going to
"
2: "↵3
↵00:00:08,051 --> 00:00:12,310
↵be talking about a topic that's extremely important topic in my mind and
"

这不是想要的结果。我对 Javascript 仍然很陌生,我想做的是将每个标题读入数组,然后循环获取开始时间和结束时间以及标题,以便我可以在 JW Player JS API 中使用它们。

最佳答案

这最终对我有用。

$.get('http://dev.sharepoint-videos.com/test.vtt', function(data) {
     
     // Read all captions into an array
     var items = data.split('\n\r\n');
     
     console.log(items);
    
     //Loop through all captions
     $.each(items, function( index, value ) {
      
      var item = items[index].split('\n');
      console.log(item);    

      });
 });

关于javascript - 如何使用 Javascript 将 VTT 文件读入数组和循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32662080/

相关文章:

javascript - 如何使 div 粘在顶部,然后在页面滚动到特定滚动时取消粘住

javascript - 从函数内的数据库检索数据

c++ - 如何将 std::vector<std::string> 作为 char** 传递?

javascript - Atom PHP/JavaScript 引用包

javascript - 如果每个循环内未设置变量,如何显示某些内容?

javascript - 当我设置超时运行时,如何不监听 mouseenter 和 mouseleave 事件?

javascript - jQuery .attr (“disabled”, “disabled”)在 IE 10 中不起作用

javascript - 如何停止功能 doajax_red 和 doajax_blue ,当检查单选按钮?

Javascript 数组未定义

C++从排序的对象数组创建排序数组的最佳方法