video.vue 1.9 KB
Newer Older
郭胜强 已提交
1 2 3
<template>
	<view>
		<page-head :title="title"></page-head>
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
4 5
		<view class="uni-padding-wrap uni-common-mt">
			<view>
郭胜强 已提交
6
				<video id="myVideo" src="https://www.dcloud.io/uniapp/wap2appvsnative.mp4" @error="videoErrorCallback" :danmu-list="danmuList"
X
xiaoyucoding 已提交
7
				 enable-danmu danmu-btn controls></video>
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
8 9 10 11 12 13 14
			</view>
			<view class="uni-list uni-common-mt">
				<view class="uni-list-cell">
					<view>
						<view class="uni-label">弹幕内容</view>
					</view>
					<view class="uni-list-cell-db">
X
xiaoyucoding 已提交
15
						<input v-model="danmuValue" class="uni-input" type="text" placeholder="在此处输入弹幕内容" />
郭胜强 已提交
16 17 18
					</view>
				</view>
			</view>
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
19
			<view class="uni-btn-v">
X
xiaoyucoding 已提交
20
				<button @click="sendDanmu" class="page-body-button">发送弹幕</button>
蓝色的小猫咪's avatar
蓝色的小猫咪 已提交
21
			</view>
郭胜强 已提交
22 23
		</view>
	</view>
X
xiaoyucoding 已提交
24
	</view>
郭胜强 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
</template>
<script>
	export default {
		data() {
			return {
				title: 'video',
				src: '',
				danmuList: [{
						text: '第 1s 出现的弹幕',
						color: '#ff0000',
						time: 1
					},
					{
						text: '第 3s 出现的弹幕',
						color: '#ff00ff',
						time: 3
					}
X
xiaoyucoding 已提交
42 43
				],
				danmuValue: ''
郭胜强 已提交
44 45
			}
		},
X
xiaoyucoding 已提交
46
		onReady: function(res) {
郭胜强 已提交
47 48 49
			this.videoContext = uni.createVideoContext('myVideo')
		},
		methods: {
X
xiaoyucoding 已提交
50
			sendDanmu: function() {
郭胜强 已提交
51
				this.videoContext.sendDanmu({
X
xiaoyucoding 已提交
52
					text: this.danmuValue,
郭胜强 已提交
53
					color: this.getRandomColor()
X
xiaoyucoding 已提交
54 55
				});
				this.danmuValue = '';
郭胜强 已提交
56
			},
X
xiaoyucoding 已提交
57
			videoErrorCallback: function(e) {
郭胜强 已提交
58
				uni.showModal({
X
xiaoyucoding 已提交
59 60
					content: e.target.errMsg,
					showCancel: false
郭胜强 已提交
61 62
				})
			},
X
xiaoyucoding 已提交
63
			getRandomColor: function() {
郭胜强 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
				const rgb = []
				for (let i = 0; i < 3; ++i) {
					let color = Math.floor(Math.random() * 256).toString(16)
					color = color.length == 1 ? '0' + color : color
					rgb.push(color)
				}
				return '#' + rgb.join('')
			}
		}
	}
</script>

<style>
X
xiaoyucoding 已提交
77 78 79 80
	video {
		width: 690upx;
	}
</style>