HarmonyOS鸿蒙Next中mp4转码代码调用canIUse("SystemCapability.Multimedia.Media.AVTranscoder")方法返回false,流程无法继续

HarmonyOS鸿蒙Next中mp4转码代码调用canIUse(“SystemCapability.Multimedia.Media.AVTranscoder”)方法返回false,流程无法继续 需要做mp4转码,代码调用 canIUse("SystemCapability.Multimedia.Media.AVTranscoder") 方法,直接返回了 false,流程无法继续 选取相册视频并对视频文件做转码压缩处理

3 回复

手机端不支持SystemCapability.Multimedia.Media.AVTranscoder,手机端支持的系统能力列表如下:

手机-系统能力SystemCapability列表-API参考概述 - 华为HarmonyOS开发者 (huawei.com)

您可以用三方库:[@ohos](/user/ohos)/videocompressor,压缩后的文件在DevEco studio–> Device File Browser > date --> el2 --> 100 --> base --> 项目地址 --> haps --> entry --> files下查看,demo如下:

import { CompressorResponseCode, CompressQuality, VideoCompressor } from '[@ohos](/user/ohos)/videoCompressor';
import picker from '[@ohos](/user/ohos).file.picker';

[@Entry](/user/Entry)
[@Component](/user/Component)
struct PhotoPickerPage {
  [@State](/user/State) message: string = 'Hello World';
  private selectVideoText: string = "选择视频";
  private highTextTest: string = "高质量压缩测试"
  private mediaTextTest: string = "中质量压缩测试"
  private lowTextTest: string = "低质量压缩测试"
  startColor: Color = Color.Blue
  [@State](/user/State) selectFilePath: string = ""

  selectVideo() {
    let that = this;
    try {
      let photoSelectOptions = new picker.PhotoSelectOptions();
      photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.VIDEO_TYPE;
      photoSelectOptions.maxSelectNumber = 1;
      let photoPicker = new picker.PhotoViewPicker();
      photoPicker.select(photoSelectOptions).then((PhotoSelectResult) => {
        that.selectFilePath = PhotoSelectResult.photoUris[0];
        console.info('videoCompressor select selectFilePath:' + that.selectFilePath)
      }).catch((err: object) => {
        console.error("videoCompressor select failed with err:" + err);
      })
    } catch (err) {
      console.error("videoCompressor select failed with err2:" + err);
    }
  }

  build() {
    Row() {
      Column() {
        Text(this.selectFilePath).fontSize(18).margin({ top: 30 }).fontWeight(FontWeight.Bold);
        Button(this.selectVideoText)
          .fontSize(20)
          .margin({ top: 30 })
          .height(50)
          .backgroundColor(this.startColor)
          .width('80%')
          .onClick(() => {
            console.log("selectVideo");
            this.selectVideo();
          })
        TextInput({ placeholder: '请输入应用内存文件名,例如www.mp4' })
          .fontSize(20)
          .margin({ top: 30 })
          .width('80%')
          .onChange((value) => {
            this.selectFilePath = getContext().filesDir + "/" + value
          })
        Button(this.highTextTest)
          .fontSize(20)
          .margin({ top: 30 })
          .height(50)
          .backgroundColor(this.startColor)
          .width('80%')
          .onClick(() => {
            let videoCompressor = new VideoCompressor();
            videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_HIGH)
              .then((data) => {
                if (data.code == CompressorResponseCode.SUCCESS) {
                  console.log("videoCompressor HIGH message:" + data.message + "--outputPath:" + data.outputPath);
                } else {
                  console.log("videoCompressor HIGH code:" + data.code + "--error message:" + data.message);
                }
              }).catch((err: Error) => {
                console.log("videoCompressor HIGH get error message" + err.message);
              })
          })
        Button(this.mediaTextTest)
          .fontSize(20)
          .margin({ top: 30 })
          .height(50)
          .backgroundColor(this.startColor)
          .width('80%')
          .onClick(() => {
            let videoCompressor = new VideoCompressor();
            videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_MEDIUM)
              .then((data) => {
                if (data.code == CompressorResponseCode.SUCCESS) {
                  console.log("videoCompressor MEDIUM message:" + data.message + "--outputPath:" + data.outputPath);
                } else {
                  console.log("videoCompressor MEDIUM code:" + data.code + "--error message:" + data.message);
                }
              }).catch((err: Error) => {
                console.log("videoCompressor HIGH get error message" + err.message);
              })
          })
        Button(this.lowTextTest)
          .fontSize(20)
          .margin({ top: 30 })
          .height(50)
          .backgroundColor(this.startColor)
          .width('80%')
          .onClick(() => {
            let videoCompressor = new VideoCompressor();
            videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_LOW)
              .then((data) => {
                if (data.code == CompressorResponseCode.SUCCESS) {
                  console.log("videoCompressor LOW message:" + data.message + "--outputPath:" + data.outputPath);
                } else {
                  console.log("videoCompressor LOW code:" + data.code + "--error message:" + data.message);
                }
              }).catch((err: Error) => {
                console.log("videoCompressor LOW get error message" + err.message);
              })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中mp4转码代码调用canIUse("SystemCapability.Multimedia.Media.AVTranscoder")方法返回false,流程无法继续的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,canIUse("SystemCapability.Multimedia.Media.AVTranscoder")方法返回false,表明当前设备或系统环境不支持AVTranscoder能力。AVTranscoder是鸿蒙系统提供的音视频转码能力,用于处理音视频文件的转码操作。返回false可能由于以下原因:

  1. 设备不支持:当前设备硬件或系统版本不支持AVTranscoder能力。
  2. 系统版本过低:AVTranscoder能力可能仅在较高版本的鸿蒙系统中提供,当前系统版本过低。
  3. 未正确配置:开发环境或项目配置中未正确启用AVTranscoder能力。

需检查设备系统版本、项目配置以及鸿蒙官方文档,确认AVTranscoder能力的支持情况。

在HarmonyOS鸿蒙Next中,canIUse("SystemCapability.Multimedia.Media.AVTranscoder")返回false,表明当前设备不支持AVTranscoder能力。建议采取以下步骤排查:

  1. 检查设备能力:确认设备是否具备多媒体转码能力。
  2. 查看API版本:确保使用的API版本支持AVTranscoder
  3. 权限配置:检查是否在config.json中正确配置了相关权限。
  4. 替代方案:如果设备不支持,考虑使用其他转码库或云端转码服务。

若问题持续,建议查阅官方文档或联系技术支持。

回到顶部