save-video-to-photos-album.uvue 1.6 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 20 21 22 23 24 25 26 27
      <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: (_) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28
            console.log("saveVideoToPhotosAlbum success");
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
29 30 31 32 33 34
            uni.showToast({
              position: "center",
              icon: "none",
              title: "视频保存成功,请到手机相册查看"
            });
          },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
35
          fail: (err) => {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
36
            uni.showModal({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
37 38
              title: "保存视频到相册失败",
              content: JSON.stringify(err),
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
39 40 41 42 43 44 45 46 47 48 49
              showCancel: false
            });
          }
        });
      }
    },
    onReady() {
      uni.showLoading({
        title: '视频下载中'
      });
      uni.downloadFile({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
50
        url: 'https://qiniu-web-assets.dcloud.net.cn/uni-app-x/static/video/swiper-vertical-video/uts.mp4',
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        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>