HarmonyOS 鸿蒙Next 使用media.AVPlayer创建实例提示Property 'createAVPlayer' does not exist on type 'typeof media'

HarmonyOS 鸿蒙Next 使用media.AVPlayer创建实例提示Property ‘createAVPlayer’ does not exist on type ‘typeof media’

import router from '@ohos.router';
import media from '@ohos.multimedia.media';

interface Song {
  title: string;
  singer: string;
  coverImgUrl: string;
  musicUrl: string;
}

interface RouteParams {
  songParam: Song;
}

@Entry
@Component
struct PlayPage {
  @State songParam: Song = {
    title: '',
    singer: '',
    coverImgUrl: '',
    musicUrl: ''
  };

  @State avPlayer: any = null;

  async aboutToAppear() {
    const params = router.getParams() as unknown as RouteParams;
    if (params && params.songParam) {
      this.songParam = params.songParam;
      await this.initAVPlayer();
    }
  }

  async initAVPlayer() {
    try {
      this.avPlayer = await media.createAVPlayer();
      console.info('createAVPlayer success');
      this.setupPlayerEvents();
      await this.preparePlayer();
    } catch (error) {
      console.error('createAVPlayer failed:', error);
      this.avPlayer = null;
    }
  }

报错信息Property 'createAVPlayer' does not exist on type 'typeof media'. 
  setupPlayerEvents() {
    if (this.avPlayer) {
      this.avPlayer.on('stateChange', (state, reason) => {
        console.info(`AVPlayer state changed to ${state}`);

      });

      this.avPlayer.on('error', (err) => {
        console.error('AVPlayer error:', err);
      });
    }
  }

  async preparePlayer() {
    if (this.avPlayer) {
      try {
        await this.avPlayer.prepare();
        console.log('AVPlayer prepared');
      } catch (err) {
        console.error('AVPlayer prepare error:', err);
      }
    }
  }

  play() {
    if (this.avPlayer && !this.avPlayer.isPlaying) {
      this.avPlayer.play();
    }
  }

  pause() {
    if (this.avPlayer && this.avPlayer.isPlaying) {
      this.avPlayer.pause();
    }
  }

  stop() {
    if (this.avPlayer) {
      this.avPlayer.stop();
    }
  }

  async aboutToDisappear() {
    if (this.avPlayer) {
      await this.stop();
      this.avPlayer.release();
      this.avPlayer = null;
    }
  }

更多关于HarmonyOS 鸿蒙Next 使用media.AVPlayer创建实例提示Property 'createAVPlayer' does not exist on type 'typeof media'的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

HarmonyOS NEXT 中可以定义为 any 类型?

检查下这行代码,换为明确的类型试试:

@State avPlayer: any = null;

更多关于HarmonyOS 鸿蒙Next 使用media.AVPlayer创建实例提示Property 'createAVPlayer' does not exist on type 'typeof media'的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,media.AVPlayer的API可能发生了变化,导致createAVPlayer方法不再直接存在于media模块中。根据鸿蒙的官方文档,AVPlayer的创建方式可能已经更新为使用createAVPlayer方法的替代方案,或者需要通过其他方式初始化AVPlayer实例。

建议查阅最新的鸿蒙开发者文档,确认AVPlayer的正确创建方式。通常,鸿蒙的API会随着版本更新进行调整,开发者需要根据最新的API规范进行开发。如果createAVPlayer方法已被弃用,文档中会提供新的方法或类来替代。

此外,确保开发环境中的SDK版本与目标设备的鸿蒙系统版本匹配,以避免API不兼容的问题。如果问题仍然存在,可以检查代码中是否存在拼写错误或导入错误,确保正确引用了media模块。

该错误提示表明在 media 模块中找不到 createAVPlayer 方法。请检查以下可能的原因:

  1. 版本兼容性:确保你使用的HarmonyOS SDK版本支持 media.AVPlayer
  2. 模块导入:确认是否正确导入了 media 模块。
  3. API变更:查阅最新的官方文档,确认 createAVPlayer 方法是否已被更新或替换为其他方法。
  4. 拼写错误:检查代码中是否有拼写错误。

建议参考官方文档或示例代码,确保使用方法正确。

回到顶部