HarmonyOS 播放本地音频文件

在项目的rawfile文件夹下放了一个a_video.wav文件,需要调用api播放这个文件,以下是我使用的方式,无法播放。报错:

err : {"code":5400102,"name":"BusinessError"} 
 
let fd = getContext().resourceManager.getRawFdSync('u_a_video.wav') 
let avPlayer: media.AVPlayer = await media.createAVPlayer(); 
this.setAVPlayerCallback(avPlayer) 
let fdPath = "fd://" + '' + fd.fd 
avPlayer.url = fdPath 
avPlayer.play((err)=>{ 
  hilog.debug(1,"sdfjsdlkfjsldkf","err : "+JSON.stringify(err)) 
})
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

应用中的媒体资源使用fdSrc,您参考以下Demo:

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(`ring.wav`); 
        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.mXComponentController.setXComponentSurfaceSize({ 
            surfaceWidth: 1920, 
            surfaceHeight: 1080 
          }); 
          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
微信
回复
3天前
相关问题
怎么读取本地音频文件列表?
6132浏览 • 1回复 待解决
鸿蒙如何将音频文件转成文本
4339浏览 • 1回复 待解决
HarmonyOS音频播放问题
318浏览 • 1回复 待解决
HarmonyOS 音频录制与播放
488浏览 • 1回复 待解决
HarmonyOS 音频播放帧率控制
64浏览 • 1回复 待解决
HarmonyOS 音频播放相关问题咨询
82浏览 • 1回复 待解决
HarmonyOS 音频后台播放问题
320浏览 • 1回复 待解决
SoundPool实现音频播放功能
1205浏览 • 1回复 待解决
HarmonyOS实现后台播放音频
325浏览 • 1回复 待解决
HarmonyOS 音频播放设备的切换
78浏览 • 1回复 待解决
HarmonyOS 本地lottie动画无法播放
293浏览 • 1回复 待解决
AVplayer开发音频播放功能
1072浏览 • 1回复 待解决
OH _Audio播放音频问题
1976浏览 • 1回复 待解决
HarmonyOS 如何判断音频是否在播放
271浏览 • 1回复 待解决