uniapp轮播图内嵌视频并实现自动播放/停止

需求:

轮播图加载了视频后自动播放视频,视频播放完成后,切换到下张图。

现有的swiper在嵌了video之后出现了各种各样的问题,比如切换下一张/切换回视频显示不全。

所以打算自己另外写一个轮播组件,不使用任何swiper组件。

中心思想就是直接硬切,轮播区域有且仅有一个元素,也就不存在video层级高遮挡的问题。

另外加上了uView的Transition组件,使得切换的时候没有那么生硬。

至于轮播指示器,可以考虑使用cover-view,然后简单布局就完成了

布局部分:

<template>
    <view style="height: 700rpx;">
        <view style="height: 700rpx;">
            <block v-for="(item, imgUrlsIndex) in imgUrls" 
            :key="imgUrlsIndex">
                <u-transition :show="!checkIsVideo(item) && imgUrlsIndex === currents" mode="fade-left"
                    class="slide-image" duration="500">
                    <image :src="item" />
                </u-transition>
                <u-transition :show="checkIsVideo(item) && imgUrlsIndex === currents" mode="fade-left"
                    class="slide-image" duration="500">
                    <video :show-fullscreen-btn="false" :src="item" :id="`myVideo${imgUrlsIndex}`" @ended="playEnded"
                        :initial-time="0" controls style="width: 100%;height: 100%;" />
                </u-transition>
            </block>
        </view>
    </view>
</template>

js部分:

<script>
    import {
        DEVICE_API_URL
    } from "@/config";
    export default {
        name: "ProductConSwiper",
        props: {
            imgUrls: {
                type: Array,
                default: () => [] //就是个资源url数组
            }
        },
        data() {
            let that = this;
            return {
                currents: 0,
                currentVideo: null,
                videoIndex: [],
                speed: 5000
                
            };
        },
        methods: {
            //判断视频
            checkIsVideo(url) {
                if (url.indexOf('.') != -1) {
                    var ext = url.substring(url.lastIndexOf('.') + 1);
                    return ['mp4', 'webm', 'mpeg4', 'ogg'].indexOf(ext) != -1
                }
            },
            // 视频播放完毕切换下一个
            playEnded() {
                this.currents += 1
                this.stopVideo()
            },
            // 播放视频
            playVideo(index = 0) {
                this.$nextTick(() => {
                    const id = `myVideo${index}`
                    this.currentVideo = uni.createVideoContext(id, this)
                    this.currentVideo.play()
                })
            },
            // 停止播放视频
            stopVideo(index = 0) {
                this.$nextTick(() => {
                    const id = `myVideo${index}`
                    this.currentVideo = uni.createVideoContext(id, this)
                    this.currentVideo.stop()
                })
            },
        },
        mounted() {
            this.imgUrls.forEach((url, index) => {
                if (this.checkIsVideo(url)) this.videoIndex.push(index)
            })
            if (!this.checkIsVideo(this.imgUrls[0])) setTimeout(() => this.currents = 1, this.speed)
            else if (this.checkIsVideo(this.imgUrls[0])) this.playVideo()
        },
        watch: {
            currents(current) {
                if (this.videoIndex.indexOf(current) > -1) this.playVideo(current)                     
                else {
                    if (this.checkIsVideo(this.imgUrls[current - 1])) this.currentVideo.stop();
                    let next = current + 1
                    if (current === this.imgUrls.length - 1) next = 0
                    setTimeout(() => this.currents = next, this.speed)
                }
            }
        }
    };
</script>

至于css部分就按各自实际场景搭建吧

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值