123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <template>
- <view class="flex justify-center align-center" :style="{ width: `${width}px` }">
- <video :id="`video${videoId}`" ref="`video${videoId}`" class="flex justify-center align-center"
- :style="{ height: `${height}px`, width: `${width}px` }" loop :src="src" :controls="false"
- :show-play-btn="false" :enable-progress-gesture="false" :object-fit="videoFit" @timeupdate="timeupdate"
- @error="playError" />
- <view class="position-absolute" v-if="!play && !OpenCover" @click="videoPlay">
- <text class="icon"></text>
- </view>
- <!-- 封面图片 -->
- <!-- 上阴影 -->
- <view class="video-top" />
- <!-- 下阴影 -->
- <view class="video-bottom" />
- </view>
- </template>
- <script>
- export default {
- props: {
- videoId: {
- type: Number,
- default: 0,
- },
- src: {
- type: String,
- default: "",
- },
- play: {
- type: Boolean,
- default: false,
- },
- height: {
- type: Number,
- default: 0,
- },
- width: {
- type: Number,
- default: 0,
- },
- objectFit: {
- type: String,
- default: "cover",
- },
- coverUrl: {
- //视频封面的图片
- type: String,
- default: "",
- },
- initialTime: {
- type: Number,
- default: 0,
- },
- gDuration: {
- type: Number,
- default: 999,
- },
- },
- data() {
- return {
- videoFit: "contain", // 优化HBuilder展示bug
- time: 0, // 时间类
- duration: 0, // 视频时长
- OpenCover: true, // 是否开启封面
- initialize: false, // 初始化
- videoTimer: null, // 计时器
- };
- },
- mounted() {
- this.videoFit = this.objectFit;
- // #ifdef APP-PLUS
- let sys = uni.getSystemInfoSync();
- if (sys.platform === "ios") {
- if (this.objectFit === "cover") {
- this.videoFit = "fill";
- }
- }
- // #endif
- let video = `video${this.videoId}`;
- console.log("this.videoId", this.videoId);
- this.videoContext = uni.createVideoContext(video, this);
- setTimeout(() => {
- this.videoContext.play();
- }, 10);
- },
- methods: {
- playError(err) {
- console.log("播放出错", err)
- },
- continuePlay() {
- console.log("继续播放")
- if (!this.initialize) {
- setTimeout(() => {
- this.initialize = true;
- this.videoPlay();
- }, 100);
- }
- },
- // 拖动滑块 1.0.9临时
- // changeCurrent(e) {
- // this.time = e.detail.value;
- // this.videoContext.seek(this.time);
- // },
- timeupdate(event) {
- // 1.0.9临时
- this.duration = event.detail.duration;
- if (this.time >= event.detail.duration) this.time = 0;
- this.time = event.detail.currentTime;
- },
- videoPlay() {
- console.log("视频播放事件", this.videoTimer);
- console.log("视频播放事件play", this.play);
- if (this.videoTimer) {
- clearTimeout(this.videoTimer);
- this.videoTimer = null;
- }
- this.videoTimer = setTimeout(() => {
- if (this.play) {
- console.log("视频播放触发");
- this.videoContext.play();
- this.OpenCover = false;
- } else {
- this.videoContext.pause();
- this.OpenCover = true;
- // 1.0.9临时
- // this.$emit('pause', this.time);
- }
- });
- // if (this.play) {
- // this.videoContext.play();
- // this.OpenCover = false;
- // } else {
- // this.videoContext.pause();
- // this.$emit('pause', this.time);
- // }
- },
- },
- watch: {
- //防抖 防止视频播放暂停太快
- play: function(newVal, oldVal) {
- console.log("newval_play", newVal);
- this.videoPlay();
- },
- // 1.0.9临时
- // startTime: {
- // immediate: true,
- // handler(newVal, oldVal) {
- // this.time = newVal;
- // }
- // },
- gDuration: {
- immediate: true,
- handler(newVal, oldVal) {
- this.duration = newVal;
- },
- },
- },
- computed: {
- barWidth() {
- let width = (this.time / this.duration) * parseInt(this.width);
- return `${width}px`;
- },
- // 1.0.9临时
- // startTime() {
- // return this.initialTime;
- // }
- },
- };
- </script>
- <style lang="scss" scoped>
- @import "@/static/css/common.css";
- .position-absolute {
- background: #000;
- }
- .icon {
- opacity: 0.6;
- font-size: 120rpx;
- color: #fff;
- }
- .icon-spin {
- //#ifndef APP-PLUS-NVUE
- animation: spin 1.5s infinite linear;
- //#endif
- }
- .video-top {
- position: absolute;
- top: 0;
- background-image: linear-gradient(to top,
- rgba(0, 0, 0, 0),
- rgba(0, 0, 0, 0.4));
- width: 750rpx;
- height: 300rpx;
- }
- .video-bottom {
- position: absolute;
- bottom: 0;
- background-image: linear-gradient(to top,
- rgba(0, 0, 0, 0.4),
- rgba(0, 0, 0, 0));
- width: 750rpx;
- height: 300rpx;
- }
- .slider-view {
- position: absolute;
- left: 0;
- bottom: 30px;
- width: 750rpx;
- }
- .progressBar {
- border-radius: 2rpx;
- height: 4rpx;
- background-color: rgba(255, 255, 255, 0.3);
- z-index: 999999;
- position: absolute;
- left: 0;
- bottom: 0;
- //#ifndef APP-PLUS-NVUE
- animation: flicker 4s linear infinite;
- animation-direction: alternate;
- //#endif
- }
- /* #ifndef APP-PLUS-NVUE */
- @keyframes flicker {
- 0% {
- box-shadow: 0 0 0 #ffffff;
- }
- /** 暂停效果 */
- 10% {
- box-shadow: 0 0 2upx #ffffff;
- }
- 50% {
- box-shadow: 0 0 10upx #ffffff;
- }
- 60% {
- box-shadow: 0 0 12upx #ffffff;
- }
- 90% {
- box-shadow: 0 0 18upx #ffffff;
- }
- 100% {
- box-shadow: 0 0 20upx #ffffff;
- }
- }
- @keyframes spin {
- 0% {
- -webkit-transform: rotate(0);
- transform: rotate(0);
- }
- 100% {
- -webkit-transform: rotate(359deg);
- transform: rotate(359deg);
- }
- }
- /* #endif */
- </style>
|