media.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <div>
  3. <!-- @timeupdate="onPlayerTimeupdate($event)" -->
  4. <video-player
  5. class="video-player vjs-custom-skin"
  6. ref="videoPlayer"
  7. :playsinline="true"
  8. @pause="onPlayerPause($event)"
  9. @play="onPlayerStart($event)"
  10. @ready="playerReadied"
  11. @ended="onPlayerEnded($event)"
  12. :globalOptions="{controls:true}"
  13. :options="options">
  14. </video-player>
  15. <div class="dialog-footer pt30">
  16. <el-row class="media-footer">
  17. <el-col :span="10" class="media-time">
  18. <span>{{curTimes|useTime}}</span>
  19. <strong>/</strong>
  20. <span>{{media.duration|useTime}}</span>
  21. </el-col>
  22. <el-col :span="4" class="media-center">
  23. <el-button class="bicon" v-if="!onPlay" type="primary" @click="doPlay" icon="el-icon-video-play" circle></el-button>
  24. <el-button class="bicon" v-else type="warning" @click="doPause" icon="el-icon-video-pause" circle></el-button>
  25. </el-col>
  26. <el-col :span="10" class="media-select">
  27. <el-button class="bicon" type="danger" icon="el-icon-close" @click="onClose" style="float: right;" >
  28. </el-button>
  29. <div style="margin-top:0px;float: right;">
  30. <el-select placeholder="流畅" v-model="selectMediaType" class="media-el-select" @change="(val)=>{$emit('changeMedia', val)}">
  31. <el-option label="流畅" value="ld" ></el-option>
  32. <el-option label="标清" value="hls"></el-option>
  33. </el-select>
  34. </div>
  35. </el-col>
  36. </el-row>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. import {httpServer} from "@/components/httpServer/httpServer.js";
  42. import md5 from 'js-md5';
  43. import {videoPlayer } from 'vue-video-player';
  44. import 'video.js/dist/video-js.css'
  45. import { MessageBox } from "element-ui";
  46. export default {
  47. name: "Index",
  48. data() {
  49. return {
  50. timer: false,
  51. tickNum: 0,
  52. prevTime:0,
  53. isReady:false,
  54. selectMediaType:'hls',
  55. curTimes:0,
  56. onPlay: false
  57. }
  58. },
  59. components: {
  60. videoPlayer
  61. },
  62. watch:{
  63. mediaType( ){
  64. this.selectMediaType = this.mediaType;
  65. if( !this.mediaType ) return;
  66. },
  67. dialog( showDilog ){
  68. if( !showDilog){
  69. this.doPause()
  70. this.isReady = false
  71. }
  72. }
  73. },
  74. props: {
  75. mediaType:{
  76. type: String,
  77. default: ''
  78. },
  79. media:{
  80. type: Object,
  81. default: ()=>{
  82. return {id: '', percent:0}
  83. }
  84. },
  85. dialog:{
  86. type: Boolean,
  87. default: true
  88. },
  89. options: {
  90. type: Object,
  91. default: () => {
  92. return {}
  93. }
  94. }
  95. },
  96. filters:{
  97. useTime(val){
  98. let timestr = ""
  99. let hour = parseInt(val/3600);
  100. let min = parseInt(val/60 % 60);
  101. let sec = parseInt(val%60);
  102. if( hour <10) hour = "0"+hour;
  103. if( min <10) min = "0"+min;
  104. if( sec <10) sec = "0"+sec;
  105. return hour+":"+min+":"+sec
  106. }
  107. },
  108. beforeDestroy() {
  109. this.stopTick()
  110. this.reportErr("play", 'destroy');
  111. },
  112. computed: {
  113. player() {
  114. return this.$refs.videoPlayer.player
  115. }
  116. },
  117. created() {
  118. this.startTick()
  119. // this.startMonitor()
  120. },
  121. methods: {
  122. startTick(){
  123. let tick = this.tryTick;
  124. if( this.timer ) clearInterval(this.timer);
  125. this.timer = setInterval(tick, 5 * 1000);
  126. },
  127. stopTick(){
  128. if( this.timer ) clearInterval(this.timer);
  129. },
  130. tryTick(){
  131. let that = this;
  132. try {
  133. that.tick()
  134. }catch(err){
  135. that.reportErr("play", ''+err.message )
  136. }
  137. },
  138. playerReadied(player) {
  139. let that = this;
  140. if( this.media.position){
  141. this.setposition( this.media.position );
  142. }else{
  143. setTimeout( ()=>{
  144. that.doPlay()
  145. }, 1000 )
  146. }
  147. this.isReady = true
  148. },
  149. onPlayerTimeupdate( player ){
  150. let myPlayer = this.$refs.videoPlayer.player;
  151. let curTimes = player.cache_.currentTime;
  152. this.curTimes = curTimes||0
  153. if( this.media.isFinish ){
  154. return;
  155. }
  156. },
  157. setposition( position ) {
  158. if( position > this.media.duration) position = this.media.duration;
  159. let player = this.$refs.videoPlayer.player;
  160. player.currentTime( position );
  161. if( this.media.isFinish) return;
  162. if( this.media.position >= this.media.duration-10 && !this.media.isFinish){
  163. this.tick( true )
  164. }
  165. this.onPlay = true
  166. },
  167. onPlayerPause(event) {
  168. this.reportErr("play", 'pause' );
  169. this.stopTick()
  170. this.onPlay = false
  171. },
  172. onPlayerEnded( event ){
  173. this.reportErr("play", 'end' );
  174. this.tick( true )
  175. },
  176. onClose(){
  177. this.reportErr("play", 'close')
  178. this.doPause()
  179. this.$emit("close")
  180. },
  181. doPause( ){
  182. this.stopTick()
  183. let myPlayer = this.$refs.videoPlayer.player;
  184. myPlayer && myPlayer.pause()
  185. this.onPlay = false
  186. },
  187. doPlay(){
  188. this.onPlay = true
  189. this.startTick();
  190. if( !this.$refs.videoPlayer || !this.$refs.videoPlayer.player ) return;
  191. if( !this.dialog ) return this.doPause();
  192. let myPlayer = this.$refs.videoPlayer.player;
  193. myPlayer && myPlayer.play()
  194. this.tickNum = 0
  195. },
  196. onPlayerStart() {
  197. this.reportErr("play", 'start');
  198. this.startTick();
  199. this.onPlay = true
  200. },
  201. startMonitor() {
  202. let that = this
  203. document.addEventListener("visibilitychange", function() {
  204. // || document.hidden
  205. if( document.visibilityState == "hidden"){
  206. that.doPause( )
  207. }else{
  208. that.doPlay()
  209. }
  210. });
  211. },
  212. // startMonitor() {
  213. // let that = this
  214. // window.onblur = function() {
  215. // that.doPause()
  216. // }
  217. // window.onfocus = function () {
  218. // that.doPlay()
  219. // }
  220. // },
  221. tickWait(){
  222. this.doPause()
  223. let that = this
  224. MessageBox({
  225. title: "连续学习超15分钟",
  226. message: "是否继续学习",
  227. showCancelButton: true,
  228. confirmButtonText: "确定",
  229. cancelButtonText: "取消",
  230. beforeClose: (action, instance, done) => {
  231. if (action === "confirm") {
  232. that.doPlay();
  233. done();
  234. } else{
  235. done();
  236. }
  237. }
  238. })
  239. },
  240. reportErr( action, msg ){
  241. httpServer("course.report", {action, msg})
  242. },
  243. tick( force = false) {
  244. let media = this.media;
  245. this.tickNum ++
  246. // 已经完成
  247. if( this.media.isFinish ) {
  248. console.log("finish")
  249. return;
  250. }
  251. // 主动暂停
  252. let myPlayer = this.$refs.videoPlayer.player;
  253. let curTimes = parseInt(myPlayer.currentTime());
  254. // 后退无心跳
  255. if( this.media.position > curTimes && !force ) return;
  256. if( curTimes< 4) {
  257. console.log("curTimes")
  258. return;
  259. }
  260. let isFinish = force?1:0
  261. if( curTimes >= media.duration ) isFinish = 1;
  262. // 拉到后面
  263. if( !isFinish ){
  264. if ( !this.onPlay ) return;
  265. }
  266. // 强制完成
  267. let param ={id: media.id, position:curTimes, isFinish};
  268. httpServer("course.tick", param, true).then( res => {
  269. if (res.code == 200) {
  270. let {skip, position, pause, closed} = res.data
  271. if( pause || closed ) {
  272. this.doPause();
  273. this.$emit("close")
  274. if( closed ) {
  275. this.$message.errorMsg("课程关闭学习", 5);
  276. }else if( pause ){
  277. this.$message.errorMsg("多处同时播放视频", 5);
  278. }
  279. return
  280. }
  281. if( !skip ) this.setposition( position );
  282. Object.assign(param, res.data )
  283. this.$emit("update", param )
  284. }
  285. })
  286. }
  287. }
  288. }
  289. </script>
  290. <style>
  291. .video-js{
  292. .vjs-control-bar{
  293. .vjs-icon-custombutton {
  294. font-family: VideoJS;
  295. font-weight: normal;
  296. font-style: normal;
  297. }
  298. .vjs-icon-custombutton:before {
  299. content: "\f108";
  300. font-size: 1.8em;
  301. line-height: 1.67;
  302. }
  303. }
  304. }
  305. .p-process{
  306. width: 100%;
  307. margin:20px auto;
  308. height: 30px;
  309. }
  310. .media-footer{
  311. padding: 0px 30px;
  312. text-align: left;
  313. line-height: 40px !important;
  314. bottom: -10px;
  315. }
  316. .media-center{
  317. text-align: center;
  318. padding: 0px;
  319. }
  320. .media-time{
  321. font-size: 18px;
  322. vertical-align: center;
  323. }
  324. .media-select{
  325. white-space:nowrap;
  326. text-align: right;
  327. line-height: 40px !important;
  328. float: right;
  329. margin: 0px !important;
  330. }
  331. .bicon{
  332. font-size: 28px !important;
  333. padding: 4px !important;
  334. }
  335. .media-el-select {
  336. font-size: 28px !important;
  337. width: 80px;
  338. padding: -4px auto !important;
  339. }
  340. </style>