HarmonyOS 如何实现拿到视频url保存视频到相册,需要支持实现暂停,恢复,取消,进度条功能

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

参考demo:

import { BusinessError, request } from '@kit.BasicServicesKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { fileIo } from '@kit.CoreFileKit';
import promptAction from '@ohos.promptAction';

@Entry
@Component
struct VideoCreateComponent {
  @State src: string = 'xxxxx'
  controller: VideoController = new VideoController()
  @State isVisibile:Visibility=Visibility.Hidden
  @State isDownload:boolean = false
  @State total:number = 0
  @State value:number = 0

  private downloadTask:request.DownloadTask | undefined = undefined
  build() {
    Column() {
      Video({
        src: this.src,
        controller: this.controller
      })
        .width('100%')
        .height('60%')
        .autoPlay(true)
        .controls(true)
        .onStart(() => {
          console.info('onStart')
        })
        .onPause(() => {
          console.info('onPause')
        })
        .onFinish(() => {
          console.info('onFinish')
        })
        .visibility(this.isVisibile)
        .onStart(() => {
          this.isVisibile=Visibility.Visible
        })

      SaveButton().onClick(async () =>{
        let context = getContext(this)
        let filePath = context.cacheDir + '/test.mp4'

        fileIo.access(filePath).then(async (result: boolean) => {
          if (result) {
            this.value = 0
            this.total = 0
            await fileIo.unlink(filePath)
          }
        })

        try {
          promptAction.showToast({
            message:"开始下载"
          })

          let helper = photoAccessHelper.getPhotoAccessHelper(context);
          // onClick触发后5秒内通过createAsset接口创建图片文件,5秒后createAsset权限收回。
          let uri = await helper.createAsset(photoAccessHelper.PhotoType.VIDEO, 'mp4');
          // 使用uri打开文件,可以持续写入内容,写入过程不受时间限制
          let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
          request.downloadFile(context, { url: this.src ,filePath:filePath}).then((data: request.DownloadTask) => {
            this.downloadTask = data;
            let progressCallback = (receivedSize: number, totalSize: number) => {
              if (!this.isDownload){
                this.isDownload = !this.isDownload
              }
              if (this.total===0) {
                this.total = totalSize
              }
              this.value = receivedSize
            };
            this.downloadTask.on('progress', progressCallback);
            this.downloadTask.on('complete', async () => {
              await fileIo.copyFile(filePath,file.fd,0).then(() =>{
                promptAction.showToast({
                  message:"下载成功,已保存到相册"
                })
              })
              await fileIo.close(file.fd)
            })
          }).catch((err: BusinessError) => {
            console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`);
          })
        }
        catch (error) {
          const err: BusinessError = error as BusinessError;
          console.error(`Failed to save photo. Code is ${err.code}, message is ${err.message}`);
        }
      })

      Button('暂停').onClick(() => {
        this.downloadTask!.suspend().then((result: boolean) => {
          console.log(`${result}`)
          promptAction.showToast({
            message:'下载暂停'
          })
        })
      })

      Button('恢复下载').onClick(() => {
        this.downloadTask!.restore().then((result: boolean) => {
          console.log(`${result}`)
          this.total = 0
          this.value = 0
          this.isDownload = !this.downloadTask
          promptAction.showToast({
            message:'正在下载'
          })
        })
      })

      Button('移除下载').onClick(() => {
        this.downloadTask!.delete().then((result: boolean) => {
          console.log(`${result}`)
          promptAction.showToast({
            message:'取消下载成功'
          })
        })
      })

      if (this.isDownload){
        Progress({ value: this.value, total: this.total, type: ProgressType.Ring })
      }
    }
    .align(Alignment.BottomEnd).width('100%').height('100%').backgroundColor(Color.White)
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 怎样实现进度条
253浏览 • 1回复 待解决
如何实现带刻度的进度条
766浏览 • 1回复 待解决
如何实现带图片的进度条
905浏览 • 1回复 待解决
Progress进度条如何实现渐变色?
1050浏览 • 1回复 待解决
视频进度滑动的三种实现方式
1804浏览 • 1回复 待解决
弧形进度条实现,有人知道方法吗?
845浏览 • 1回复 待解决
怎么实现保存网络图片相册功能
710浏览 • 1回复 待解决
HarmonyOS 进度条样式
37浏览 • 1回复 待解决
HarmonyOS 实现保存图片相册
275浏览 • 1回复 待解决
如何实现一个月食样式的进度条
443浏览 • 1回复 待解决
实现一个发送进度条通知的方法
519浏览 • 1回复 待解决
HarmonyOS 多彩色进度条展示
116浏览 • 1回复 待解决