判断否可以使用flash
function checkFlash() {
if (typeof window.ActiveXObject !== 'undefined') {
// window.open('http://www.adobe.com/go/getflash')
return !!new ActiveXObject('ShockwaveFlash.ShockwaveFlash')
} else {
return !!navigator.plugins['Shockwave Flash']
}
}
var u = checkFlash()
if(!u){
this.initRtmpPlayer(this.info)
}else{
this.initHlsPlayer(this.info)
}
initRtmpPlayer(data) {
this.player = new TcPlayer(this.id, {
'm3u8': data.hls,
'rtmp': data.rtmp,
'autoplay': this.autoplay,
'coverpic': '',
'width': '100%',
'height': '100%',
'live': true,
wording: {
1002: ' 无法使用内网视频流,已切换到外网'
},
listener: function (msg) {
if (!this.playState && msg.type === 'timeupdate') {
this.playState = true
}
if (msg.type === 'error') {
if (!this.runOnce) {
this.player.options.src = this.rtmp
document.querySelector('.vcp-playtoggle').click()
this.runOnce = true
}
}
}.bind(this)
})
// 一段时间内没有播放,则尝试切换源播放
setTimeout(function () {
if (this.playState === false) {
let start = document.querySelector('.vcp-playtoggle')
start && start.click()
setTimeout(function () {
let err = document.querySelector('.vcp-error-tips')
err && (err.style.display = 'none')
}, 4000)
}
}.bind(this), 4000)
},
initHlsPlayer(data) {
if (Hls.isSupported()) {
var video = document.querySelector(`#${this.id} video`)
this.hlsPlayer = new Hls()
this.hlsPlayer.attachMedia(video)
this.hlsPlayer.on(Hls.Events.MEDIA_ATTACHED, function () {
this.hlsPlayer.loadSource(data.hls)
this.hlsPlayer.on(Hls.Events.FRAG_PARSED, function () {
// 如果不是自动播放,则只加载一桢
if (!this.runOnce && !this.autoplay) {
this.hlsPlayer.stopLoad()
this.runOnce = true
}
}.bind(this))
}.bind(this))
video.addEventListener('play', function () {
this.hlsPlayer.startLoad(-1)
}.bind(this))
video.addEventListener('pause', function () {
this.hlsPlayer.stopLoad()
}.bind(this))
}else{
video.style.display = "block";
video.src = data.hls
}
}
使用rtmp进行播放
网友评论