save-video-to-photos-album.uvue 1.6 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <video class="video" :src="src"></video>
      <button type="primary" class="margin-top-10" @click="saveVideo">将视频保存到手机相册</button>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: 'saveVideoToPhotosAlbum',
        src: ''
      }
    },
    methods: {
      saveVideo() {
        uni.saveVideoToPhotosAlbum({
          filePath: this.src,
          success: (_) => {
            uni.showToast({
              position: "center",
              icon: "none",
              title: "视频保存成功,请到手机相册查看"
            });
          },
          fail: (e) => {
            uni.showModal({
              content: "保存相册失败,errCode:" + e.errCode + ",errMsg:" + e.errMsg + ",errSubject:" + e.errSubject,
              showCancel: false
            });
          }
        });
      }
    },
    onReady() {
      uni.showLoading({
        title: '视频下载中'
      });
      uni.downloadFile({
        url: 'https://qiniu-web-assets.dcloud.net.cn/video/sample/2minute-demo.mp4',
        success: (res) => {
          console.log("download video success", res.tempFilePath);
          this.src = res.tempFilePath;
          uni.hideLoading();
        }
      });
    }
  }
</script>

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

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