HarmonyOS AVPlayer的url居然不支持放本地文件路径?

async getProxyUrl(url: string): Promise<string> {
  let tempUrl = await GlobalProxyServer?.getInstance()?.getServer()?.getProxyUrl(url)
  let proxyUrl: string | undefined = tempUrl ? tempUrl : url
  if (!proxyUrl.startsWith("http://") && !proxyUrl.startsWith("https://")) {
  let file = fs.openSync(proxyUrl, fs.OpenMode.READ_ONLY);
  proxyUrl = "fd://" + file.fd
}
return proxyUrl
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

示例参考如下:

import { media } from '@kit.MediaKit';
import { BusinessError } from '@kit.BasicServicesKit';

const TAG = 'AVPlayerDemo';

@Entry
@Component
struct Index {
  private surfaceId: string = ''; // surfaceId,用于关联XComponent与视频播放器
  private mXComponentController: XComponentController = new XComponentController();
  private avPlayer: media.AVPlayer | undefined = undefined;

  aboutToAppear(): void {
    //创建avplayer
    this.createAVPlayer()
  }

  createAVPlayer() {
    media.createAVPlayer().then((video: media.AVPlayer) => {
      if (video != null) {
        this.avPlayer = video;
        this.setAVPlayerCallback(this.avPlayer);
        //加载本地文件
        let fileDescriptor = getContext().resourceManager.getRawFdSync('videoTest.mp4');
        let avFileDescriptor: media.AVFileDescriptor = {
          fd: fileDescriptor.fd,
          offset: fileDescriptor.offset,
          length: fileDescriptor.length
        };
        this.avPlayer.fdSrc = avFileDescriptor

        console.info(TAG,'createAVPlayer success');
      } else {
        console.error(TAG,'createAVPlayer fail');
      }
    }).catch((error: BusinessError) => {
      console.error(TAG,`AVPlayer catchCallback, error message:${error.message}`);
    });
  }
  
  // 注册avplayer回调函数
  setAVPlayerCallback(avPlayer: media.AVPlayer) {
    avPlayer.on('timeUpdate', (time: number) => {
      console.info(TAG,`AVPlayer timeUpdate. time is ${time}`);
    })
    // error回调监听函数,当avPlayer在操作过程中出现错误时调用 reset接口触发重置流程
    avPlayer.on('error', (err: BusinessError) => {
      console.error(TAG,`Invoke avPlayer failed, code is ${err.code}, message is ${err.message}`);
      avPlayer.reset(); // 调用reset重置资源,触发idle状态
    })
    // 状态机变化回调函数
    avPlayer.on('stateChange', async (state: string, reason: media.StateChangeReason) => {
      switch (state) {
        case 'idle': // 成功调用reset接口后触发该状态机上报
          console.info(TAG,'AVPlayer state idle called.');
          avPlayer.release(); // 调用release接口销毁实例对象
          break;
        case 'initialized': // avplayer 设置播放源后触发该状态上报
          console.info(TAG,'AVPlayer state initialized called.');
          avPlayer.surfaceId = this.surfaceId;
          avPlayer.prepare();
          break;
        case 'prepared': // prepare调用成功后上报该状态机
          console.info(TAG,'AVPlayer state prepared called.');
          avPlayer.setSpeed(media.PlaybackSpeed.SPEED_FORWARD_1_00_X)
          avPlayer.seek(1, media.SeekMode.SEEK_PREV_SYNC)
          break;
        case 'completed': // prepare调用成功后上报该状态机
          console.info(TAG,'AVPlayer state completed called.');
          break;
        case 'playing': // play成功调用后触发该状态机上报
          console.info(TAG,'AVPlayer state playing called.');
          break;
        case 'paused': // pause成功调用后触发该状态机上报
          console.info(TAG,'AVPlayer state paused called.');
          break;
        case 'stopped': // stop接口成功调用后触发该状态机上报
          console.info(TAG,'AVPlayer state stopped called.');
          break;
        case 'released':
          console.info(TAG,'AVPlayer state released called.');
          break;
        default:
          console.info(TAG,'AVPlayer state unknown called.');
          break;
      }
    })
  }

  build() {
    Column() {
      XComponent({
        id: 'PiPWindow',
        type: XComponentType.SURFACE,
        controller: this.mXComponentController
      })
        .onLoad(() => {
          this.surfaceId = this.mXComponentController.getXComponentSurfaceId();
        })
        .width('100%')
        .height(260)
      
      Button('播放')
        .width(200)
        .height(50)
        .onClick(() => {
          this.avPlayer?.play()
        })
    }
    .zIndex(1)
    .width('100%')
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
本地模拟器不支持断点调试????
2642浏览 • 1回复 待解决
鸿蒙工程不支持aar本地导入吗?
8492浏览 • 1回复 已解决
HarmonyOS SVG文件不支持<text>标签吗
52浏览 • 1回复 待解决
HarmonyOS avplayer播放fd路径文件报错
80浏览 • 1回复 待解决
web组件不支持localstorage
839浏览 • 1回复 待解决
HarmonyOS TextSpan不支持align
32浏览 • 1回复 待解决
@BuilderParam 不支持普通class变量
807浏览 • 1回复 待解决
HarmonyOS RN使用datetimePicker显示不支持
113浏览 • 1回复 待解决
HarmonyOS Span不支持n换行
45浏览 • 1回复 待解决
HarmonyOS image不支持mask吗
57浏览 • 1回复 待解决
文件上传本地路径如何获取
453浏览 • 1回复 待解决
http类不支持cancel方法
209浏览 • 1回复 待解决
Panel不支持水平方向拖拉
678浏览 • 1回复 待解决