media.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <div>
  3. <video-player
  4. class="video-player vjs-custom-skin"
  5. ref="videoPlayer"
  6. :playsinline="true"
  7. @pause="onPlayerPause($event)"
  8. @play="onPlayerStart($event)"
  9. @timeupdate="onPlayerTimeupdate($event)"
  10. @ready="playerReadied"
  11. @ended="onPlayerEnded($event)"
  12. :globalOptions="{controls:true}"
  13. :options="options">
  14. </video-player>
  15. <div class="dialog-footer">
  16. <el-row class="media-footer">
  17. <el-col :span="8" class="media-time">
  18. <span>{{curTimes|useTime}}</span>
  19. <strong>/</strong>
  20. <span>{{media.duration|useTime}}</span>
  21. </el-col>
  22. <el-col :span="8" 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="8" 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. console.log("change mediaType", this.mediaType)
  65. this.selectMediaType = this.mediaType;
  66. if( !this.mediaType ) return;
  67. },
  68. dialog( showDilog ){
  69. if( !showDilog){
  70. this.doPause()
  71. this.isReady = false
  72. }
  73. }
  74. },
  75. props: {
  76. mediaType:{
  77. type: String,
  78. default: ''
  79. },
  80. media:{
  81. type: Object,
  82. default: ()=>{
  83. return {id: '', percent:0}
  84. }
  85. },
  86. dialog:{
  87. type: Boolean,
  88. default: true
  89. },
  90. options: {
  91. type: Object,
  92. default: () => {
  93. return {}
  94. }
  95. }
  96. },
  97. filters:{
  98. useTime(val){
  99. let timestr = ""
  100. let hour = parseInt(val/3600);
  101. let min = parseInt(val/60 % 60);
  102. let sec = parseInt(val%60);
  103. if( hour <10) hour = "0"+hour;
  104. if( min <10) min = "0"+min;
  105. if( sec <10) sec = "0"+sec;
  106. return hour+":"+min+":"+sec
  107. }
  108. },
  109. beforeDestroy() {
  110. if (this.timer) {
  111. clearInterval(this.timer); //关闭
  112. }
  113. },
  114. computed: {
  115. player() {
  116. return this.$refs.videoPlayer.player
  117. }
  118. },
  119. created() {
  120. this.timer = setInterval(this.tick, 5 * 1000);
  121. // this.startMonitor();
  122. },
  123. methods: {
  124. playerReadied(player) {
  125. // console.log("playerReadied", this.media.position);
  126. let that = this;
  127. if( this.media.position){
  128. this.setposition( this.media.position );
  129. }else{
  130. setTimeout( ()=>{
  131. that.doPlay()
  132. }, 1000 )
  133. }
  134. this.isReady = true
  135. },
  136. onPlayerTimeupdate( player ){
  137. let myPlayer = this.$refs.videoPlayer.player;
  138. let curTimes = player.cache_.currentTime;
  139. this.curTimes = curTimes||0
  140. if( this.media.isFinish ){
  141. return;
  142. }
  143. if( curTimes>30 && curTimes > this.media.position+15 && !this.media.isFinish){
  144. console.log("return", curTimes, this.media.position )
  145. player.currentTime( this.media.position );
  146. return;
  147. }
  148. },
  149. setposition( position ) {
  150. if( position > this.media.duration) position = this.media.duration;
  151. let player = this.$refs.videoPlayer.player;
  152. player.currentTime( position );
  153. if( this.media.isFinish) return;
  154. if( this.media.position >= this.media.duration-30 && !this.media.isFinish){
  155. this.tick( true )
  156. }
  157. this.onPlay = true
  158. },
  159. onPlayerPause(event) {
  160. this.onPlay = false
  161. },
  162. onPlayerEnded( event ){
  163. this.tick( true )
  164. },
  165. onClose(){
  166. this.doPause()
  167. this.$emit("close")
  168. },
  169. doPause( ){
  170. let myPlayer = this.$refs.videoPlayer.player;
  171. myPlayer && myPlayer.pause()
  172. console.log("doPause", myPlayer)
  173. this.onPlay = false
  174. },
  175. doPlay(){
  176. this.onPlay = true
  177. console.log("doPlay")
  178. if( !this.$refs.videoPlayer || !this.$refs.videoPlayer.player ) return;
  179. if( !this.dialog ) return this.doPause();
  180. let myPlayer = this.$refs.videoPlayer.player;
  181. myPlayer && myPlayer.play()
  182. this.tickNum = 0
  183. },
  184. onPlayerStart() {
  185. this.onPlay = true
  186. },
  187. startMonitor() {
  188. let that = this
  189. window.onblur = function() {
  190. that.doPause()
  191. }
  192. window.onfocus = function () {
  193. that.doPlay()
  194. }
  195. },
  196. tickWait(){
  197. this.doPause()
  198. let that = this
  199. MessageBox({
  200. title: "连续学习超15分钟",
  201. message: "是否继续学习",
  202. showCancelButton: true,
  203. confirmButtonText: "确定",
  204. cancelButtonText: "取消",
  205. beforeClose: (action, instance, done) => {
  206. if (action === "confirm") {
  207. that.doPlay();
  208. done();
  209. } else{
  210. done();
  211. }
  212. }
  213. })
  214. },
  215. tick( force = false) {
  216. let media = this.media;
  217. this.tickNum ++
  218. // 已经完成
  219. if( this.media.isFinish ) return;
  220. // 主动暂停
  221. let myPlayer = this.$refs.videoPlayer.player;
  222. let curTimes = parseInt(myPlayer.currentTime());
  223. // 后退无心跳
  224. if( this.media.position > curTimes && !force ) return;
  225. if( curTimes< 4) return;
  226. let isFinish = force?1:0
  227. if( curTimes >= media.duration ) isFinish = 1;
  228. // 拉到后面
  229. if( !isFinish ){
  230. if ( !this.onPlay ) return;
  231. if( curTimes > this.prevTime){
  232. this.prevTime = curTimes
  233. return;
  234. }
  235. }
  236. // 强制完成
  237. let param ={id: media.id, position:curTimes ,isFinish};
  238. httpServer("course.tick", param, true).then( res => {
  239. if (res.code == 200) {
  240. let {skip, position, pause} = res.data
  241. if( pause ) {
  242. this.doPause();
  243. this.$emit("close")
  244. return
  245. }
  246. if( !skip ) this.setposition( position );
  247. Object.assign(param, res.data )
  248. this.$emit("update", param )
  249. }
  250. }).catch( res =>{
  251. console.log( "tick", res)
  252. })
  253. }
  254. }
  255. }
  256. </script>
  257. <style>
  258. .video-js{
  259. .vjs-control-bar{
  260. .vjs-icon-custombutton {
  261. font-family: VideoJS;
  262. font-weight: normal;
  263. font-style: normal;
  264. }
  265. .vjs-icon-custombutton:before {
  266. content: "\f108";
  267. font-size: 1.8em;
  268. line-height: 1.67;
  269. }
  270. }
  271. }
  272. .p-process{
  273. width: 100%;
  274. margin:20px auto;
  275. height: 30px;
  276. }
  277. .media-footer{
  278. padding: 0px 30px;
  279. text-align: left;
  280. line-height: 40px !important;
  281. bottom: -10px;
  282. }
  283. .media-center{
  284. text-align: center;
  285. padding: 0px;
  286. }
  287. .media-time{
  288. font-size: 21px;
  289. vertical-align: center;
  290. }
  291. .media-select{
  292. white-space:nowrap;
  293. text-align: right;
  294. line-height: 40px !important;
  295. float: right;
  296. margin: 0px !important;
  297. }
  298. .bicon{
  299. font-size: 28px !important;
  300. padding: 4px !important;
  301. }
  302. .media-el-select {
  303. font-size: 28px !important;
  304. width: 80px;
  305. padding: -4px auto !important;
  306. }
  307. </style>