video.uvue 23.9 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1
<template>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
2 3 4 5 6 7 8 9 10 11 12
  <view class="uni-flex-item">
    <video class="video" ref="video" id="video" :header="header" :src=src :autoplay="autoplay" :loop="loop"
      :muted="muted" :initial-time="initialTime" :duration="duration" :controls="controls" :danmu-btn="danmuBtn"
      :enable-danmu="enableDanmu" :page-gesture="pageGesture" :direction="direction" :show-progress="showProgress"
      :show-fullscreen-btn="showFullscreenBtn" :show-play-btn="showPlayBtn" :show-center-play-btn="showCenterPlayBtn"
      :show-loading="showLoading" :enable-progress-gesture="enableProgressGesture" :object-fit="objectFit"
      :poster="poster" :show-mute-btn="showMuteBtn" :title="title" :enable-play-gesture="enablePlayGesture"
      :vslide-gesture="vslideGesture" :vslide-gesture-in-fullscreen="vslideGestureInFullscreen" :codec="codec"
      :http-cache="httpCache" :play-strategy="playStrategy" :danmu-list="danmuList" @play="onPlay" @pause="onPause"
      @ended="onEnded" @timeupdate="onTimeUpdate" @waiting="onWaiting" @error="onError" @progress="onProgress"
      @fullscreenclick="onFullScreenClick" @controlstoggle="onControlsToggle" @fullscreenchange="onFullScreenChange">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
13
      <!-- #ifdef APP-ANDROID -->
14 15
      <image v-if="subCompEnable && subCompShow" class="img-fast-backward" src="../../../static/test-video/fast-backward.png" @tap="fastBackward"></image>
      <image v-if="subCompEnable && subCompShow" class="img-fast-forward" src="../../../static/test-video/fast-forward.png" @tap="fastForward"></image>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
16
      <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
17
    </video>
雪洛's avatar
雪洛 已提交
18
    <scroll-view class="uni-padding-wrap uni-common-mt uni-flex-item">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
19
      <view class="uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
20
        <navigator url="/pages/component/video/video-format">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
21
          <button type="primary" @click="pause">视频格式示例</button>
22
        </navigator>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
23
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
24
      <!-- #ifdef APP-ANDROID -->
25 26 27
      <view class="uni-flex uni-btn-v" style="justify-content: space-between;align-items: center;">
        <text class="uni-title" style="width: 80%;">子组件实现快进、快退功能(全屏后显示)</text>
        <switch :checked="subCompEnable" @change="onSubCompEnableChange" />
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28 29
      </view>
      <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
30 31 32
      <view class="uni-title">
        <text class="uni-title-text">API示例</text>
      </View>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
33
      <view class="uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
34 35 36 37 38 39
        <button type="primary" @click="play">播放</button>
      </view>
      <view class="uni-btn-v">
        <button type="primary" @click="pause">暂停</button>
      </view>
      <view class="uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
40
        <input class="input" placeholder="输入要跳转的位置,单位s" type="number" @input="onSeekInput"></input>
41
        <button type="primary" @click="seek(this.pos)">跳转到指定位置</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42 43
      </view>
      <view class="uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44 45
        <enum-data title="选择进入全屏时的视频方向" :items="directionItemTypes" @change="onRequestFullScreenDirectionChange"></enum-data>
        <button type="primary" @click="requestFullScreen">进入全屏</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
46 47 48 49 50 51 52 53
      </view>
      <view class="uni-btn-v">
        <button type="primary" @click="exitFullScreen">退出全屏</button>
      </view>
      <view class="uni-btn-v">
        <button type="primary" @click="stop">停止</button>
      </view>
      <view class="uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
54 55
        <input class="input" placeholder="输入弹幕" value="{ 'text': '要显示的文本', 'color': '#FF0000' }" type="string" @input="onSendDanmuInput"></input>
        <button type="primary" :disabled="!enableDanmu" @click="sendDanmu">发送弹幕</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
56 57
      </view>
      <view class="uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
58 59
        <enum-data title="选择倍速" :items="rateItemTypes" @change="onPlaybackRateChange"></enum-data>
        <button type="primary" @click="playbackRate">设置倍速</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
60 61 62 63
      </view>
      <view class="uni-title">
        <text class="uni-title-text">属性示例</text>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      <input class="input margin-10" type="string" placeholder="设置播放资源" @confirm="onSrcComfirm"></input>
      <input class="input margin-10" type="number" placeholder="设置初始播放位置(播放前设置有效)" @confirm="onInitialTimeComfirm"></input>
      <input class="input margin-10" type="number" placeholder="设置视频时长(播放前设置有效)" @confirm="onDurationComfirm"></input>
      <input class="input margin-10" type="string" placeholder="设置视频封面" @confirm="onPosterComfirm"></input>
      <input class="input margin-10" type="string" placeholder="设置视频标题(仅限非 Web 平台)" @confirm="onTitleComfirm"></input>
      <input class="input margin-10" type="string" placeholder="设置header(json格式)" @confirm="onHeaderComfirm"></input>
      <boolean-data title="设置是否展示弹幕(播放前设置有效)" :defaultValue="enableDanmu" @change="onEnableDanmuChange"></boolean-data>
      <boolean-data title="设置是否自动播放(播放前设置有效)" :defaultValue="autoplay" @change="onAutoplayChange"></boolean-data>
      <boolean-data title="设置是否循环播放(播放完成后生效)" :defaultValue="loop" @change="onLoopChange"></boolean-data>
      <boolean-data title="设置是否静音播放" :defaultValue="muted" @change="onMutedChange"></boolean-data>
      <boolean-data title="设置是否显示默认播放控件" :defaultValue="controls" @change="onControlsChange"></boolean-data>
      <boolean-data title="设置是否显示弹幕按钮" :defaultValue="danmuBtn" @change="onDanmuBtnChange"></boolean-data>
      <boolean-data title="设置是否显示进度条" :defaultValue="showProgress" @change="onShowProgressChange"></boolean-data>
      <boolean-data title="设置是否显示全屏按钮" :defaultValue="showFullscreenBtn" @change="onShowFullscreenBtnChange"></boolean-data>
      <boolean-data title="设置是否显示视频底部控制栏的播放按钮" :defaultValue="showPlayBtn" @change="onShowPlayBtnChange"></boolean-data>
      <boolean-data title="设置是否显示静音按钮(仅限非 Web 平台)" :defaultValue="showMuteBtn" @change="onShowMuteBtnChange"></boolean-data>
      <enum-data title="设置全屏时视频的方向" :items="directionItemTypes" @change="onDirectionChange"></enum-data>
      <boolean-data title="设置是否显示视频中间的播放按钮" :defaultValue="showCenterPlayBtn" @change="onShowCenterPlayBtnChange"></boolean-data>
      <boolean-data title="设置是否显示loading控件" :defaultValue="showLoading" @change="onShowLoadingChange"></boolean-data>
      <boolean-data title="设置是否开启控制进度的手势" :defaultValue="enableProgressGesture" @change="onEnableProgressGestureChange"></boolean-data>
      <boolean-data title="设置是否开启播放手势(仅限非 Web 平台)" :defaultValue="enablePlayGesture" @change="onEnablePlayGestureChange"></boolean-data>
fxy060608's avatar
fxy060608 已提交
85
      <!-- #ifndef WEB -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
86
      <boolean-data title="非全屏模式下,设置是否开启亮度与音量调节手势" :defaultValue="pageGesture" @change="onPageGestureChange"></boolean-data>
fxy060608's avatar
fxy060608 已提交
87
      <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
88 89 90 91 92 93
      <boolean-data title="非全屏模式下,设置是否开启亮度与音量调节手势(仅限非 Web 平台)" :defaultValue="vslideGesture" @change="onVslideGestureChange"></boolean-data>
      <boolean-data title="全屏模式下,设置是否开启亮度与音量调节手势(仅限非 Web 平台)" :defaultValue="vslideGestureInFullscreen" @change="onVslideGestureInFullscreenChange"></boolean-data>
      <boolean-data title="设置是否对http、https视频源开启本地缓存(仅 App 平台,播放前设置有效)" :defaultValue="httpCache" @change="onHttpCacheChange"></boolean-data>
      <enum-data title="设置视频大小与video容器大小不一致时,视频的表现形式" :items="objectFitItemTypes" @change="onObjectFitChange"></enum-data>
      <enum-data title="设置解码器(仅 App 平台,播放前设置有效)" :items="codecItemTypes" @change="onCodecChange"></enum-data>
      <enum-data title="设置播放策略(仅 App 平台,播放前设置有效)" :items="playStrategyItemTypes" @change="onPlayStrategyChange"></enum-data>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
94
    </scroll-view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
95
  </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
96 97
</template>

H
hdx 已提交
98
<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
99
  import { ItemType } from '@/components/enum-data/enum-data';
100 101 102
  // #ifdef APP-ANDROID
  import ViewGroup from 'android.view.ViewGroup';
  // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
103
  export default {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
104
    onReady() {
W
wanganxp 已提交
105 106
      this.videoContext = uni.createVideoContext('video');
      // this.videoContext = uni.createVideoContext('video', this);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
107
    },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
108 109
    data() {
      return {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
110 111
        videoContext: null as VideoContext | null,
        // 属性
雪洛's avatar
雪洛 已提交
112
        src: "https://qiniu-web-assets.dcloud.net.cn/video/sample/2minute-demo.mp4",
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
113 114 115 116
        autoplay: false,
        loop: false,
        muted: false,
        initialTime: 0,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
117
        duration: 0,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        controls: true,
        danmuList: [{
          text: '要显示的文本',
          color: '#FF0000',
          time: 3
        }, {
          text: '要显示的文本2',
          color: '#31ff23',
          time: 5
        }, {
          text: '要显示的文本3',
          color: '#f13ef8',
          time: 7
        }, {
          text: '要显示的文本4',
          color: '#4972f8',
          time: 9
        }, {
          text: '要显示的文本5',
          color: '#000000',
          time: 11
        }] as Array<Danmu>,
        danmuBtn: false,
        enableDanmu: true,
        pageGesture: false,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
143
        direction: -1,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
144 145
        directionItemTypes: [{ "value": 0, "name": "0(正常竖向)" }, { "value": 1, "name": "90(屏幕逆时针90度)" }, { "value": 2, "name": "-90(屏幕顺时针90度)" }] as ItemType[],
        directionItems: [0, 90, -90],
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
146 147 148 149 150 151 152
        showProgress: true,
        showFullscreenBtn: true,
        showPlayBtn: true,
        showCenterPlayBtn: true,
        showLoading: true,
        enableProgressGesture: true,
        objectFit: "contain",
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
153 154
        objectFitItemTypes: [{ "value": 0, "name": "contain(包含)" }, { "value": 1, "name": "fill(填充)" }, { "value": 2, "name": "cover(覆盖)" }] as ItemType[],
        objectFitItems: ["contain", "fill", "cover"],
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
155 156 157 158 159 160 161
        poster: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-android.png",
        showMuteBtn: false,
        title: "video-component",
        enablePlayGesture: false,
        vslideGesture: false,
        vslideGestureInFullscreen: true,
        codec: "hardware",
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
162 163
        codecItemTypes: [{ "value": 0, "name": "hardware(硬解码)" }, { "value": 1, "name": "software(软解码)" }] as ItemType[],
        codecItems: ["hardware", "software"],
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
164 165
        httpCache: true,
        playStrategy: 0,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
166 167
        playStrategyItemTypes: [{ "value": 0, "name": "0(普通模式)" }, { "value": 1, "name": "1(平滑播放模式)" }, { "value": 1, "name": "2(M3U8优化模式)" }] as ItemType[],
        playStrategyItems: [0, 1, 2],
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
168
        header: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
169 170 171
          'User-Agent': 'User-Agent test',
          'header': 'header test',
          'cookie': 'cookie test'
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
172 173
        } as UTSJSONObject,
        // API
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
174
        pos: 0,
175
        requestFullScreenOptions: null as RequestFullScreenOptions | null,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
176 177 178 179
        danmu: {
          text: '要显示的文本',
          color: '#FF0000'
        } as Danmu,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
180 181 182
        rate: 1,
        rateItemTypes: [{ "value": 0, "name": "0.5" }, { "value": 1, "name": "0.8" }, { "value": 2, "name": "1.0" }, { "value": 3, "name": "1.25" }, { "value": 4, "name": "1.5" }] as ItemType[],
        rateItems: [0.5, 0.8, 1.0, 1.25, 1.5],
183 184 185 186
        subCompEnable: false,
        subCompShow: false,
        curPos: 0,
        endPos: 0,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
187
        // 自动化测试
188
        autoTest: false,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
189 190
        isPlaying: false,
        isPause: false,
191
        isError: false,
雪洛's avatar
雪洛 已提交
192 193 194 195 196 197 198 199 200 201
        eventPlay: null as UTSJSONObject | null,
        eventPause: null as UTSJSONObject | null,
        eventEnded: null as UTSJSONObject | null,
        eventTimeupdate: null as UTSJSONObject | null,
        eventFullscreenchange: null as UTSJSONObject | null,
        eventWaiting: null as UTSJSONObject | null,
        eventError: null as UTSJSONObject | null,
        eventProgress: null as UTSJSONObject | null,
        eventFullscreenclick: null as UTSJSONObject | null,
        eventControlstoggle: null as UTSJSONObject | null
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
202 203
      }
    },
雪洛's avatar
雪洛 已提交
204 205
    onLoad() {
    },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
206
    methods: {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
207 208 209 210 211 212 213
      // API
      play: function () {
        console.log("play");
        this.videoContext?.play();
      },
      pause: function () {
        console.log("pause");
雪洛's avatar
雪洛 已提交
214
        (uni.getElementById("video") as UniVideoElement).pause(); //as写法测试。注意id不对时as会崩溃
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
215 216
        // this.videoContext?.pause();
      },
217 218 219
      seek: function (pos : number) {
        console.log("seek -> " + pos);
        this.videoContext?.seek(pos);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
220
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
221 222 223 224 225 226
      onSeekInput: function (event : UniInputEvent) {
        this.pos = parseInt(event.detail.value);
      },
      requestFullScreen: function () {
        console.log("requestFullScreen -> " + this.requestFullScreenOptions);
        this.videoContext?.requestFullScreen(this.requestFullScreenOptions);
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
227 228 229 230 231 232 233 234 235 236
      },
      exitFullScreen: function () {
        console.log("exitFullScreen");
        this.videoContext?.exitFullScreen();
      },
      stop: function () {
        console.log("stop");
        uni.getElementById<UniVideoElement>("video")?.stop(); //泛型写法测试
        // this.videoContext?.stop();
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
237 238 239 240 241 242 243 244
      sendDanmu: function () {
        console.log("sendDanmu -> " + this.danmu);
        this.videoContext?.sendDanmu(this.danmu);
      },
      onSendDanmuInput: function (event : UniInputEvent) {
        let json = JSON.parse<Danmu>(event.detail.value)
        if (json == null) return;
        this.danmu = json as Danmu;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
245
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
246 247 248 249 250 251
      playbackRate: function () {
        console.log("playbackRate -> " + this.rate);
        this.videoContext?.playbackRate(this.rate);
      },
      onPlaybackRateChange: function (value : number) {
        this.rate = this.rateItems[value];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
252
      },
253 254 255 256 257 258 259 260 261 262
      fastBackward: function () {
        let pos = this.curPos - 15;
        if (pos < 0) pos = 0;
        this.seek(pos);
      },
      fastForward: function () {
        let pos = this.curPos + 15;
        if (pos > this.endPos) pos = this.endPos;
        this.seek(pos);
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
263
      // 属性
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
264 265 266 267
      onSrcComfirm: function (event : UniInputConfirmEvent) {
        let value = event.detail.value;
        if (value == '') return;
        this.src = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
268 269
        console.log("src -> " + this.src)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
270 271
      onAutoplayChange: function (value : boolean) {
        this.autoplay = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
272 273
        console.log("autoplay -> " + this.autoplay)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
274 275
      onLoopChange: function (value : boolean) {
        this.loop = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
276 277
        console.log("loop -> " + this.loop)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
278 279
      onMutedChange: function (value : boolean) {
        this.muted = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
280 281
        console.log("muted -> " + this.muted)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
282 283
      onInitialTimeComfirm: function (event : UniInputConfirmEvent) {
        let value = parseInt(event.detail.value)
雪洛's avatar
雪洛 已提交
284
        if (isNaN(value)) value = 0;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
285
        this.initialTime = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
286 287
        console.log("initialTime -> " + this.initialTime)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
288 289
      onDurationComfirm: function (event : UniInputConfirmEvent) {
        let value = parseInt(event.detail.value)
雪洛's avatar
雪洛 已提交
290
        if (isNaN(value)) value = 0;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
291
        this.duration = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
292 293
        console.log("duration -> " + this.duration)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
294 295
      onControlsChange: function (value : boolean) {
        this.controls = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
296 297
        console.log("controls -> " + this.controls)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
298 299 300 301 302 303
      onEnableDanmuChange: function (value : boolean) {
        this.enableDanmu = value;
        console.log("enableDanmu -> " + this.enableDanmu)
      },
      onDanmuBtnChange: function (value : boolean) {
        this.danmuBtn = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
304 305
        console.log("danmuBtn -> " + this.danmuBtn)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
306 307
      onPageGestureChange: function (value : boolean) {
        this.pageGesture = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
308 309
        console.log("pageGesture -> " + this.pageGesture)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
310 311 312 313 314 315 316 317
      onRequestFullScreenDirectionChange: function (value : number) {
        let direction = this.directionItems[value];
        this.requestFullScreenOptions = {
          direction
        } as RequestFullScreenOptions;
      },
      onDirectionChange: function (value : number) {
        this.direction = this.directionItems[value];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
318 319
        console.log("direction -> " + this.direction)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
320 321
      onShowProgressChange: function (value : boolean) {
        this.showProgress = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
322 323
        console.log("showProgress -> " + this.showProgress)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
324 325
      onShowFullscreenBtnChange: function (value : boolean) {
        this.showFullscreenBtn = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
326 327
        console.log("showFullscreenBtn -> " + this.showFullscreenBtn)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
328 329
      onShowPlayBtnChange: function (value : boolean) {
        this.showPlayBtn = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
330 331
        console.log("showPlayBtn -> " + this.showPlayBtn)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
332 333
      onShowCenterPlayBtnChange: function (value : boolean) {
        this.showCenterPlayBtn = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
334 335
        console.log("showCenterPlayBtn -> " + this.showCenterPlayBtn)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
336 337
      onShowLoadingChange: function (value : boolean) {
        this.showLoading = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
338 339
        console.log("showLoading -> " + this.showLoading)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
340 341
      onEnableProgressGestureChange: function (value : boolean) {
        this.enableProgressGesture = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
342 343
        console.log("enableProgressGesture -> " + this.enableProgressGesture)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
344 345
      onObjectFitChange: function (value : number) {
        this.objectFit = this.objectFitItems[value];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
346 347
        console.log("objectFit -> " + this.objectFit)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
348 349 350 351
      onPosterComfirm: function (event : UniInputConfirmEvent) {
        let value = event.detail.value;
        if (value == '') return;
        this.poster = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
352 353
        console.log("poster -> " + this.poster)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
354 355
      onShowMuteBtnChange: function (value : boolean) {
        this.showMuteBtn = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
356 357
        console.log("showMuteBtn -> " + this.showMuteBtn)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
358 359 360 361
      onTitleComfirm: function (event : UniInputConfirmEvent) {
        let value = event.detail.value;
        if (value == '') return;
        this.title = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
362 363
        console.log("title -> " + this.title)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
364 365
      onEnablePlayGestureChange: function (value : boolean) {
        this.enablePlayGesture = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
366 367
        console.log("enablePlayGesture -> " + this.enablePlayGesture)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
368 369
      onVslideGestureChange: function (value : boolean) {
        this.vslideGesture = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
370 371
        console.log("vslideGesture -> " + this.vslideGesture)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
372 373
      onVslideGestureInFullscreenChange: function (value : boolean) {
        this.vslideGestureInFullscreen = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
374 375
        console.log("vslideGestureInFullscreen -> " + this.vslideGestureInFullscreen)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
376 377
      onCodecChange: function (value : number) {
        this.codec = this.codecItems[value];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
378 379
        console.log("codec -> " + this.codec)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
380 381
      onHttpCacheChange: function (value : boolean) {
        this.httpCache = value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
382 383
        console.log("httpCache -> " + this.httpCache)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
384 385
      onPlayStrategyChange: function (value : number) {
        this.playStrategy = this.playStrategyItems[value];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
386 387
        console.log("playStrategy -> " + this.playStrategy)
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
388 389 390 391
      onHeaderComfirm: function (event : UniInputConfirmEvent) {
        let json = JSON.parse(event.detail.value)
        if (json == null) return;
        this.header = json as UTSJSONObject;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
392
        console.log("header -> " + JSON.stringify(this.header))
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
393
      },
394 395
      onSubCompEnableChange: function (event : UniSwitchChangeEvent) {
      	this.subCompEnable = event.detail.value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
396
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
397
      // 事件
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
398
      onPlay: function (res : UniEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
399 400 401
        console.log(res.type);
        this.isPlaying = true;
        this.isPause = false;
402 403 404 405 406 407
        if (this.autoTest) {
          this.eventPlay = {
            "tagName": res.target?.tagName,
            "type": res.type
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
408
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
409
      onPause: function (res : UniEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
410 411 412
        console.log(res.type);
        this.isPlaying = false;
        this.isPause = true;
413 414 415 416 417 418
        if (this.autoTest) {
          this.eventPause = {
            "tagName": res.target?.tagName,
            "type": res.type
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
419
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
420
      onEnded: function (res : UniEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
421
        console.log(res.type);
422 423 424 425 426 427
        if (this.autoTest) {
          this.eventEnded = {
            "tagName": res.target?.tagName,
            "type": res.type
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
428
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
429
      onTimeUpdate: function (res : UniVideoTimeUpdateEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
430
        console.log(res.type + " -> " + JSON.stringify(res.detail));
431 432
        this.curPos = res.detail.currentTime;
        this.endPos = res.detail.duration;
433 434 435 436
        if (this.autoTest) {
          this.eventTimeupdate = {
            "tagName": res.target?.tagName,
            "type": res.type,
437 438
            "currentTime": Math.trunc(res.detail.currentTime),
            "duration": Math.trunc(res.detail.duration)
439 440
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
441
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
442
      onFullScreenChange: function (res : UniVideoFullScreenChangeEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
443
        console.log(res.type + " -> " + JSON.stringify(res.detail));
444
        this.subCompShow = res.detail.fullScreen;
445 446 447 448 449 450 451 452
        if (this.autoTest) {
          this.eventFullscreenchange = {
            "tagName": res.target?.tagName,
            "type": res.type,
            "fullScreen": res.detail.fullScreen,
            "direction": res.detail.direction
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
453
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
454
      onWaiting: function (res : UniEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
455
        console.log(res.type);
456 457 458 459 460 461
        if (this.autoTest) {
          this.eventWaiting = {
            "tagName": res.target?.tagName,
            "type": res.type
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
462
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
463
      onError: function (res : UniVideoErrorEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
464
        console.log(res.type + " -> " + JSON.stringify(res.detail));
465
        this.isError = true;
466 467 468 469 470 471 472
        if (this.autoTest) {
          this.eventError = {
            "tagName": res.target?.tagName,
            "type": res.type,
            "errCode": res.detail.errCode
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
473
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
474
      onProgress: function (res : UniVideoProgressEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
475
        console.log(res.type + " -> " + JSON.stringify(res.detail));
476 477 478 479
        if (this.autoTest) {
          this.eventProgress = {
            "tagName": res.target?.tagName,
            "type": res.type,
480
            "isBufferedValid": res.detail.buffered >= 0
481 482
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
483
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
484
      onFullScreenClick: function (res : UniVideoFullScreenClickEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
485
        console.log(res.type + " -> " + JSON.stringify(res.detail));
486 487 488 489
        if (this.autoTest) {
          this.eventFullscreenclick = {
            "tagName": res.target?.tagName,
            "type": res.type,
490 491 492 493
            "screenX": Math.trunc(res.detail.screenX),
            "screenY": Math.trunc(res.detail.screenY),
            "screenWidth": Math.trunc(res.detail.screenWidth),
            "screenHeight": Math.trunc(res.detail.screenHeight)
494 495
          };
        }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
496
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
497
      onControlsToggle: function (res : UniVideoControlsToggleEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
498
        console.log(res.type + " -> " + JSON.stringify(res.detail));
499 500 501 502 503 504 505
        if (this.autoTest) {
          this.eventControlstoggle = {
            "tagName": res.target?.tagName,
            "type": res.type,
            "show": res.detail.show
          };
        }
506 507 508 509 510 511
      },
      // 自动化测试
      downloadSource() {
        uni.downloadFile({
          url: 'https://qiniu-web-assets.dcloud.net.cn/video/sample/2minute-demo.mp4',
          success: (res) => {
512
            this.src = res.tempFilePath;
513 514 515 516 517
          },
          fail: (_) => {
            this.isError = true;
          }
        })
518 519 520 521
      },
      // #ifdef APP-ANDROID
      hasSubComponent() : boolean {
        const view = uni.getElementById('video')?.getAndroidView<ViewGroup>();
522
        return view == null ? false : view.getChildAt(0) instanceof ViewGroup;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
523
      }
524
      // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
525 526
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
527 528 529
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
530
  .video {
H
hdx 已提交
531 532
    width: 100%;
    height: 200px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
533
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
534 535 536 537

  .input {
    height: 40px;
    background: #FFF;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
538 539 540 541 542
    padding: 8px 13px;
  }

  .margin-10 {
    margin: 10px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
543
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
544

545 546 547
  .img-fast-backward {
    width: 40px;
    height: 40px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
548
    top: 50%;
549
    left: 12%;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
550 551 552
    transform: translate(-50%, -50%);
    position: absolute;
  }
553 554 555 556 557 558 559 560 561

  .img-fast-forward {
    width: 40px;
    height: 40px;
    top: 50%;
    right: 12%;
    transform: translate(50%, -50%);
    position: absolute;
  }
562
</style>