save-video-to-photos-album.uvue 1.4 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4 5 6
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
7
      <video class="video" :src="src" :controls="true"></video>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
8 9 10 11 12 13 14 15 16 17 18 19
      <button type="primary" class="margin-top-10" @click="saveVideo">将视频保存到手机相册</button>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: 'saveVideoToPhotosAlbum',
20 21 22
        src: '/static/test-video/10second-demo.mp4',
        // 自动化测试
        success: false
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
23 24 25 26 27 28 29
      }
    },
    methods: {
      saveVideo() {
        uni.saveVideoToPhotosAlbum({
          filePath: this.src,
          success: (_) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
30
            console.log("saveVideoToPhotosAlbum success");
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
31 32 33 34 35
            uni.showToast({
              position: "center",
              icon: "none",
              title: "视频保存成功,请到手机相册查看"
            });
36
            this.success = true;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
37
          },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
38
          fail: (err) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
39
            uni.showModal({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
40 41
              title: "保存视频到相册失败",
              content: JSON.stringify(err),
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42 43
              showCancel: false
            });
44
            this.success = false;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
          }
        });
      }
    }
  }
</script>

<style>
  .video {
    align-self: center;
  }

  .margin-top-10 {
    margin-top: 10px;
  }
</style>