根據傳入的數組list來決定打開圖片還是全屏播放視頻,還有橫豎屏等初始預設參數
組件.vue
<template>
<view v-if="list">
<view class="button" @click="openMedia()">打開圖片/視頻</view>
<!-- 始終豎屏播放:direction="0" -->
<video v-show="isOpenVideo" id="myVideo" :src="list[0]" @fullscreenchange="fullscreenchange"></video>
</view>
</template>
<script>
const util = require('@/utils/utils.js')
export default {
name: "open-media",
props: {
list: {
type: Array,
default: []
},
},
data() {
return {
isOpenVideo: false,
videoContext: null
};
},
methods: {
openMedia() {
if (this.list[0].indexOf('.mp4') > 0) {
//打開視頻(全屏播放)
this.isOpenVideo = true
this.videoContext = uni.createVideoContext('myVideo', this)
this.videoContext.play()
this.videoContext.requestFullScreen()
} else {
//打開圖片
util.previewImage(this.list)
}
},
//退出全屏時停止播放
fullscreenchange(e) {
console.log(e)
if (!e.detail.fullScreen) {
this.videoContext.stop()
this.isOpenVideo = false
}
}
}
}
</script>
<style lang="scss">
.button {
width: 230rpx;
text-align: center;
padding: 10rpx 20rpx;
border: 1px solid #000741;
border-radius: 50rpx;
margin-top: 15rpx;
}
</style>
utils.js文章來源:http://www.zghlxwxcb.cn/news/detail-533841.html
/**
* 預覽圖片
* @param {Array} filePath
*/
function previewImage(filePath) {
uni.previewImage({
urls: filePath,
longPressActions: {
itemList: ['發(fā)送給朋友', '保存圖片', '收藏'],
success: res => {
console.log('選中了第' + (res.tapIndex + 1) + '個按鈕,第' + (res.index + 1) +
'張圖片')
},
fail: err => {
console.log(err.errMsg)
}
}
})
}
module.exports = {
previewImage,
};
歡迎交流~~文章來源地址http://www.zghlxwxcb.cn/news/detail-533841.html
到了這里,關于uniapp小程序點擊按鈕打開圖片/全屏播放視頻組件的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!