Introduction
外挂字幕组件,目前支持vtt格式的解析
Start
-
Install
$ npm install xgplayer-subtitles
-
Usage
Step 1:
<div id="vs"></div>
Step 2:
import Player from 'xgplayer';
const player = new Player({
id: 'vs',
url: ''
})
const options = {
player: player,
subTitles: [{
label: '中文',
language : '',
id: 'cn',
isDefault: false,
url: './subtitle/cn.vtt'
}, {
label: '英文',
url: './subtitle/en.vtt',
id: 'en',
isDefault: true,
language: 'en'
}],
domRender: true,
defaultOpen: false,
mode: 'bg',
line: 'double',
updateMode: 'vod'
renderMode: 'normal',
debugger: 'false'
}
const subTitle = new window.XgSubtitle(options)
subTitle.attachPlayer(player)
config
{
player: any,
subTitles: [{
label?: any,
language: any,
id: any,
isDefault: boolean,
url: string
list: [{
start: number,
end: number,
text: Array<string>
}],
stringContent: string
},
...
],
domRender: boolean,
defaultOpen: boolean,
mode: string,
line: string
}
API
atttchPlayer(player)
挂载播放器实例
detachPlayer(player)
卸载播放器实例
destroy
销毁组件
switch({id:'', language:''})
切换字幕. id和language只需有一个即可,用户从初始化的字幕列表中选择字幕
subtitle.switch({id: 'cn', language:'cn'}).then(() => {
console.log('切换成功')
}).catch(() => {
console.log('切换失败')
})
切换信息返回说明
{
code: 0,
msg: 'SUCCESS'
},{
code: 1,
msg: 'LOAD_ERROR'
}, {
code: 2,
msg: 'PARSER_ERROR'
}, {
code: 3,
msg: 'FORMAT_NOT_SUPPORTED'
},
{
code: 4,
msg: 'ID_OR_LANGUAGE_NOT_EXIST'
}, {
code: 5,
msg: 'PARAMETERS_ERROR'
}, {
code: 6,
msg: 'ABORT'
}, {
code: 7,
msg: 'UNKNOWN'
}
switchExt({id:'', language:''})
开启附属字幕是,字幕时间戳和switch接口开启的字幕一致,只是文本不一致
switchOff()
关闭字幕更新
setSubTitles(subTitles, isOpen)
- @param subTitles要更新的字幕列表
- @isOpen boolean 是否默认开启字幕
事件
update
字幕更新事件
subTitle.on('update', data => {
if (data.length === 0) {
} else {
}
})
off
字幕被关闭的时候触发,subtitle.switchOff()接口会触发该事件
change
字幕变化的时候触发
subTitle.on('change', data => {
console.log('current subtitle is ', data)
})
reset
字幕重置,该事件只在字幕集成在hls中的时候触发
subTitle.on('reset', data => {
console.log('the new subTitle list is ', data.list)
})