前言
最新基于uniapp开发了视频播放 app,其中个人觉得核心东西就是播放器,我在做的过程中使用的是uniapp video 组件,使用方法如下。
视频播放组件定义及使用
1. 新建一个目录命名为 template,专门存模板(此说法源于以前开发微信小程序)。新建一个 video-player.vue 文件。
2. 复制下面代码。注意点: 组件名称不要忘记定义(name: 'videoPlayer',);watch 和 mounted 属性自己慢慢琢磨。
<template>
<!-- autoplay="false" -->
<view name="videoPlayer" class="flex flex-direction">
<video id="myVideo"
class="response"
:src="videoUrl"
:title="videoTitle"
controls="true"
page-gesture="true"
object-fit="contain"
show-mute-btn="true"
enable-play-gesture="true"
vslide-gesture="true"
@error="videoErrorCallback"
@waiting="videoWaiting"
@loadedmetadata="videoLoadOk"
></video>
</view>
</template>
<script>
export default {
name: 'videoPlayer',
props: {
title: {
type: String,
default: ''
},
firstPic: {
type: String,
default: ''
},
videoType: {
type: String,
default: ''
},
videoSrc: {
type: String,
default: ''
}
},
data() {
return {
videoUrl: this.videoSrc,
firstImg: this.firstPic,
videoTitle: this.title
};
},
watch: {
videoSrc() {
this.setVideoUrl();
}
},
mounted() {
this.setVideoUrl();
},
onReady: function(res) {
// #ifndef MP-ALIPAY
this.videoContext = uni.createVideoContext('myVideo');
// #endif
},
methods: {
setVideoUrl() {
console.log(this.videoSrc)
// uni.setNavigationBarTitle({
// title: this.title
// });
this.videoUrl = this.videoSrc;
this.firstImg = this.firstPic;
this.videoTitle = this.title;
console.log(this.firstImg + '___' + this.firstPic)
console.log(this.title + '___' + this.videoTitle)
},
videoErrorCallback(e) {
uni.showModal({
content: e.target.errMsg,
showCancel: false
});
},
videoWaiting() {
// uni.showLoading({
// title: '加载中'
// });
},
videoLoadOk() {
// uni.hideLoading();
}
}
};
</script>
<style></style>
3. 组件使用:
1) script 引入: import videoPlayer from '../../template/video-player/video-player';
2) 引入组件: js 代码中添加
components: {
videoPlayer
},
3) 界面代码中添加:
<video-player :title="videoTitle" :videoSrc="videoSrc" :videoType="videoType" :firstPic="firstPic">
</video-player>
最终效果