HarmonyOS 鸿蒙Next soundPool 错误,请教

发布于 1周前 作者 phonegap100 来自 鸿蒙OS

HarmonyOS 鸿蒙Next soundPool 错误,请教

以下是一个播放代码,cke_9067.png文件路径是resfile

代码获取到路径如下/data/storage/el1/bundle/entry/resources/resfile/bchess.mp3

播放声音时候:   play sound Error: errCode is 5400102,一直这个错误

请问这段代码哪里有问题,一直不能调试成功

import { audio } from ‘@kit.AudioKit’;

import { media } from ‘@kit.MediaKit’;

import { fileIo, fileUri } from ‘@kit.CoreFileKit’;

import { BusinessError } from ‘@kit.BasicServicesKit’;

import { resourceManager } from ‘@kit.LocalizationKit’;

import Logger from ‘…/common/utils/Logger’;

import { fileIo as fs } from ‘@kit.CoreFileKit’;

export   class SoundManage{

  public context = getContext(this);

  public creatsound(context:Context){

      create(this.context)

    }

}

let soundPool: media.SoundPool;

let streamId: number = 0;

let soundId: number = 0;

let audioRendererInfo: audio.AudioRendererInfo = {

  usage: audio.StreamUsage.STREAM_USAGE_MUSIC,

  rendererFlags: 1

}

let playParameters: media.PlayParameters = {

  loop: 3, // 循环4次

  rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // 正常倍速

  leftVolume: 0.5, // range = 0.0-1.0

  rightVolume: 0.5, // range = 0.0-1.0

  priority: 1, // 最低优先级

}

let uri: string = “”;

async function create(context:Context) {

  //创建soundPool实例

  soundPool = await media.createSoundPool(5, audioRendererInfo);

    //注册监听

  loadCallback();

  finishPlayCallback();

  setErrorCallback();

  let pathDir = context.resourceDir;

  let path = pathDir + ‘/bchess.mp3’;

  console.info("file fd===path======: " + path);

  // 加载音频资源

  await fileIo.open(path, fileIo.OpenMode.READ_ONLY).then((file: fileIo.File) => {

    console.info(“file fd: " + file.fd);

    uri = ‘fd://’ + (file.fd).toString()

    console.info(“file fd=========: " + uri);

  }).catch((error: BusinessError) => {

    console.error(=========soundpool open, error message:${error.message});

  });

    await soundPool.load(uri).then((soundId: number) => {

      console.info(‘soundPool load uri success===’+soundId);

      soundId = soundId;

      PlaySoundPool(soundId);

    }).catch((err: BusinessError) => {

      console.error('soundPool load failed and catch error is ’ + err.message);

    })

}

async function PlaySoundPool(i:number) {

  // 开始播放,这边play也可带播放播放的参数PlayParameters

  console.info(‘PlaySoundPool:’ + i);

  console.info(‘PlaySoundPool:’ + soundPool);

  soundPool.play(i, playParameters, (error, streamID: number) => {

    if (error) {

      console.info(play sound Error: errCode is ${error.code}, errMessage is ${error.message})

    } else {

      streamId = streamID;

      console.info(‘play success soundid:’ + streamId);

    }

  });

  // 设置循环播放次数

  await soundPool.setLoop(streamId, 2); // 播放3次

  // 设置对应流的优先级

  await soundPool.setPriority(streamId, 1);

  // 设置音量

  await soundPool.setVolume(streamId, 0.5, 0.5);

}

function loadCallback() {

  // 加载完成回调

  soundPool.on(‘loadComplete’, (soundId_: number) => {

    console.info('loadComplete, soundId: ’ + soundId_);

  })

}

//设置播放完成监听

function finishPlayCallback() {

  // 播放完成回调

  soundPool.on(‘playFinished’, () => {

    console.info(“recive play finished message”);

    // 可进行下次播放

  })

}

//设置错误类型监听

function setErrorCallback() {

  soundPool.on(‘error’, (error: BusinessError) => {

    console.info(‘error happened,message is :’ + error.message+”=错误代码=”+error.code);

  })

}

async function release() {

  // 终止指定流的播放

  await soundPool.stop(streamId);

  // 卸载音频资源

  await soundPool.unload(soundId);

  //关闭监听

  setOffCallback();

  // 释放SoundPool

  await soundPool.release();

}

//关闭监听

function setOffCallback() {

  soundPool.off(‘loadComplete’);

  soundPool.off(‘playFinished’);

  soundPool.off(‘error’);

}



关于HarmonyOS 鸿蒙Next soundPool 错误,请教的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

4 回复

希望HarmonyOS能继续优化系统稳定性,减少崩溃和重启的情况。

加载raw中资源不能用load(uri)方法,要用load(fd)方法:

cke_1860.png

还有需要异步回调中做处理,改造后我试了可以正常播放了:

import { audio } from '[@kit](/user/kit).AudioKit';

import { media } from '[@kit](/user/kit).MediaKit';

import { fileIo, fileUri } from '[@kit](/user/kit).CoreFileKit';

import { BusinessError } from '[@kit](/user/kit).BasicServicesKit';
import { resourceManager } from '[@kit](/user/kit).LocalizationKit';


export class SoundManage{

public context = getContext(this);

public creatsound(context:Context){

create(this.context)

}

}

let soundPool: media.SoundPool;

let streamId: number = 0;

let soundId: number = 0;

let audioRendererInfo: audio.AudioRendererInfo = {

usage: audio.StreamUsage.STREAM_USAGE_MUSIC,

rendererFlags: 1

}

let playParameters: media.PlayParameters = {

loop: 3, // 循环4次

rate: audio.AudioRendererRate.RENDER_RATE_NORMAL, // 正常倍速

leftVolume: 0.5, // range = 0.0-1.0

rightVolume: 0.5, // range = 0.0-1.0

priority: 1, // 最低优先级

}

let uri: string = "";

export async function create(context:Context) {

//创建soundPool实例

soundPool = await media.createSoundPool(5, audioRendererInfo);

//注册监听

loadCallback();

// finishPlayCallback();

setErrorCallback();

let pathDir = context.resourceDir;

let path = pathDir + '/bchess.mp3';

console.info("file fd===path======: " + path);

try {
context.resourceManager.getRawFd("bchess.mp3", (error: BusinessError, value: resourceManager.RawFileDescriptor) => {
if (error != null) {
console.error(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`);
} else {
let fd = value.fd;
let offset = value.offset;
let length = value.length;
uri = 'fd://' + fd.toString();
soundPool.load(fd, offset, length).then((soundId: number) => {

console.info('soundPool load uri success==='+soundId);



}).catch((err: BusinessError) => {

console.error('soundPool load failed and catch error is ' + err.message);

})
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getRawFd failed, error code: ${code}, message: ${message}.`);
}

}

async function PlaySoundPool(i:number) {

// 开始播放,这边play也可带播放播放的参数PlayParameters

console.info('PlaySoundPool:' + i);

soundPool.play(i, playParameters, (error, streamID: number) => {

if (error) {

console.info(`play sound Error: errCode is ${error.code}, errMessage is ${error.message}`)

} else {

streamId = streamID;

console.info('play success soundid:' + streamId);

}

});

// 设置循环播放次数

// await soundPool.setLoop(streamId, 2); // 播放3次

// 设置对应流的优先级

// await soundPool.setPriority(streamId, 1);

// 设置音量

// await soundPool.setVolume(streamId, 0.5, 0.5);

}

function loadCallback() {

// 加载完成回调

soundPool.on('loadComplete', (soundId_: number) => {

console.info('loadComplete, soundId: ' + soundId_);
PlaySoundPool(soundId_);

})

}

//设置播放完成监听

function finishPlayCallback() {

// 播放完成回调

soundPool.on('playFinished', () => {

console.info("recive play finished message");

// 可进行下次播放

})

}

//设置错误类型监听

function setErrorCallback() {

soundPool.on('error', (error: BusinessError) => {

console.info('error happened,message is :' + error.message+"=错误代码="+error.code);

})

}

async function release() {

// 终止指定流的播放

await soundPool.stop(streamId);

// 卸载音频资源

await soundPool.unload(soundId);

//关闭监听

setOffCallback();

// 释放SoundPool

await soundPool.release();

}

//关闭监听

function setOffCallback() {

soundPool.off('loadComplete');

soundPool.off('playFinished');

soundPool.off('error');

}

很喜欢HarmonyOS的卡片式设计,信息一目了然,操作也更便捷。

感谢,试了一下,的确如此。

回到顶部