webRtc streaming-vue
最编程
2024-07-15 21:07:30
...
export default {
name: "WebRtcPlayer",
props: {
videoSrc: {
type: String,
default: ''
}
},
data() {
return {
player: null
}
},
mounted() {
this.$watch('videoSrc', () => {
if (this.videoSrc) {
this.initVideo(this.videoSrc)
console.log('播放视频路径:', this.videoSrc)
}
}, {immediate: true})
},
methods: {
/**
* 初始化播放器并播放
* 两种调用方式
* 一种通过 watch监听 props 传过来的 src 进行播放
* 一种通过 引用组件上的 ref 直接调用 initVideo 如 this.$refs.webrtc.initVideo('src')
* */
initVideo(url) {
// 播放器存在 清空重置
if (this.player) {
this.player.destroy()
this.player = null
}
// 获取承载元素dom
let videoDom = document.getElementById('jswebrtc')
// 初始化播放器
this.player = new JSWebrtc.Player(url,
{
video: videoDom,
autoplay: true,
onPlay: (obj) => {
// 监听video元素状态,可播放时进行播放 。 某些情况下 autoplay 会失效
// mdn https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLMediaElement/canplay_event
// 菜鸟 https://www.runoob.com/tags/av-event-canplay.html
videoDom.addEventListener('canplay', function (e) {
videoDom.play()
})
console.log(obj, '播放器开始播放!')
}
})
}
},
beforeDestroy() {
// 播放器存在清除播放器
if (this.player) {
this.player.destroy()
}
}
}
推荐阅读
-
Windows下编译WebRTC
-
使用coturn项目搭建基于stun/turn服务器的webrtc笔记(1)
-
简易教程:使用Docker、Coturn、Janus、SRS和FFmpeg搭建WebRTC服务器
-
探究Webrtc Core audio崩溃的故障案例
-
应对网络不佳的 WebRTC 音频:NACK 技术的应用
-
如何在JavaScript Web应用中强制发送PLI数据包使用WebRTC?
-
理解WebRTC SDP协商流程和SDP协议解析方法
-
常见 WebRTC 缩写和知识点分享
-
理解WebRTC的四大要点:关键帧请求与FIR/PLI之差异,以及什么是SLI(Slice Loss Indication)
-
深入理解WebRTC中的SDP协议