| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <template>
- <div id="dplayer" ref="dplayer" style="height: 480px;" />
- </template>
- <script>
- import { videoUrl } from '@/api/video'
- import SocketInstance from '@/utils/ws/socket-instance'
- import flvjs from 'flv.js'
- import DPlayer from 'dplayer'
- import Vue from "vue";
- export default {
- name: 'VideoPlayer',
- props: {
- videoProp: {
- type: Object,
- default: () => null
- }
- },
- data() {
- return {
- flvjs,
- DPlayer,
- danmaku: {
- api: '//api.reghao.cn/api/comment/danmaku/',
- token: 'bili'
- },
- getUrl: true
- }
- },
- created() {
- },
- mounted() {
- const videoId = this.videoProp.videoId
- if (this.getUrl) {
- this.getVideoUrl(videoId)
- }
- },
- methods: {
- getVideoUrl(videoId) {
- videoUrl(videoId).then(res => {
- if (res.code === 0) {
- const userdata = Vue.$cookies.get('USERDATA')
- if (userdata != null) {
- SocketInstance.connect()
- }
- const urlType = res.data.type
- if (urlType === 'mp4') {
- const urls = res.data.urls
- for (const url of urls) {
- url.type = 'normal'
- }
- this.initMp4Player(this.videoProp.userId, videoId, this.videoProp.coverUrl, urls, res.data.currentTime)
- } else if (urlType === 'flv') {
- const urls = res.data.urls
- const url = urls[0].url
- this.initFlvPlayer(videoId, this.videoProp.coverUrl, url)
- } else {
- this.$notify.error({
- message: '视频 url 类型不合法',
- type: 'warning',
- duration: 3000
- })
- }
- } else {
- this.$notify.error({
- message: '视频 url 获取失败',
- type: 'warning',
- duration: 3000
- })
- }
- }).catch(error => {
- this.$notify.error({
- message: error.message,
- type: 'error',
- duration: 3000
- })
- })
- },
- danmakuConfig() {
- // TODO 获取弹幕配置,将 videoUrl 作为本函数的回调
- },
- initMp4Player(userId, videoId, coverUrl, urls, pos) {
- const player = new DPlayer({
- container: document.querySelector('#dplayer'),
- lang: 'zh-cn',
- logo: '/logo.png',
- screenshot: false,
- autoplay: true,
- volume: 0.1,
- mutex: true,
- video: {
- pic: coverUrl,
- defaultQuality: 0,
- quality: urls,
- hotkey: true
- },
- danmaku: {
- id: videoId,
- maximum: 10000,
- api: this.danmaku.api,
- token: this.danmaku.token,
- user: userId,
- bottom: '15%',
- unlimited: true
- }
- })
- // 设置音量
- // player.volume(0.1, true, false)
- // 跳转到上次看到的位置
- player.seek(pos)
- /* 事件绑定 */
- player.on('progress', function() {
- const jsonData = {}
- jsonData.videoId = videoId
- jsonData.currentTime = player.video.currentTime
- SocketInstance.send(jsonData)
- })
- player.on('ended', () => {
- const jsonData = {}
- jsonData.videoId = videoId
- jsonData.currentTime = player.video.currentTime
- SocketInstance.send(jsonData)
- /* const path = this.$route.path
- const nextPath = '/video/gz5RYkw1zn'
- if (path !== nextPath) {
- this.$router.push(nextPath)
- } else {
- console.log('视频播放完成')
- }*/
- })
- player.on('volumechange', () => {
- console.log('声音改变')
- })
- },
- initFlvPlayer(videoId, coverUrl, videoUrl) {
- new DPlayer({
- container: document.getElementById('dplayer'),
- video: {
- url: videoUrl,
- type: 'customFlv',
- customType: {
- customFlv: function(video, player) {
- const flvPlayer = flvjs.createPlayer({
- type: 'flv',
- url: video.src
- })
- flvPlayer.attachMediaElement(video)
- flvPlayer.load()
- }
- }
- }
- })
- }
- }
- }
- </script>
- <style>
- </style>
|