新手发帖,如何动态播放资源文件里面的音频文件 cocos3.8.2版本

新手发帖,如何动态加载资源文件里面的音频文件,譬如我把一堆mp3放在资源文件夹(resources)下面,如何通过代码动态的加载进行播放,文件路径(resources\0001.mp3),感谢!!!
2.4版本使用cc.loader.loadRes(“0001”, cc.AudioClip, function (err, clip) {
cc.audioEngine.play(clip, true, 1);
});可以正常调用,但是在3.8.2里面audioEngine已经弃用了,使用了官方文档里面的音频管理类AudioMgr,但是调用的时候,每次都报错 “Bundle resources doesn’t contain voice/0001”,resources文件夹下面存放了voice文件夹,音频mp3文件存放在voice文件夹下面。

3.8.2音频管理调用代码:AudioMgr.inst.play(“voice/0001”, 1),每次运行到这里,声音无法播放,后台日志提示“Bundle resources doesn’t contain voice/0001”,文件路径如下图所示:1752303300319

官方文档音频管理类代码如下:
//AudioMgr.ts

import { Node, AudioSource, AudioClip, resources, director } from ‘cc’;

/**

  • @en

  • this is a sington class for audio play, can be easily called from anywhere in you project.

  • @zh

  • 这是一个用于播放音频的单件类,可以很方便地在项目的任何地方调用。

*/

export class AudioMgr {

private static _inst: AudioMgr;

public static get inst(): AudioMgr {

    if (this._inst == null) {

        this._inst = new AudioMgr();

    }

    return this._inst;

}

private _audioSource: AudioSource;

constructor() {

    //@en create a node as audioMgr

    //@zh 创建一个节点作为 audioMgr

    let audioMgr = new Node();

    audioMgr.name = '__audioMgr__';

    //@en add to the scene.

    //@zh 添加节点到场景

    director.getScene().addChild(audioMgr);

    //@en make it as a persistent node, so it won't be destroied when scene change.

    //@zh 标记为常驻节点,这样场景切换的时候就不会被销毁了

    director.addPersistRootNode(audioMgr);

    //@en add AudioSource componrnt to play audios.

    //@zh 添加 AudioSource 组件,用于播放音频。

    this._audioSource = audioMgr.addComponent(AudioSource);

}

public get audioSource() {

    return this._audioSource;

}

/**

 * @en

 * play short audio, such as strikes,explosions

 * @zh

 * 播放短音频,比如 打击音效,爆炸音效等

 * @param sound clip or url for the audio

 * @param volume

 */

playOneShot(sound: AudioClip | string, volume: number = 1.0) {

    if (sound instanceof AudioClip) {

        this._audioSource.playOneShot(sound, volume);

    }

    else {

        resources.load(sound, (err, clip: AudioClip) => {

            if (err) {

                console.log(err);

            }

            else {

                this._audioSource.playOneShot(clip, volume);

            }

        });

    }

}

/**

 * @en

 * play long audio, such as the bg music

 * @zh

 * 播放长音频,比如 背景音乐

 * @param sound clip or url for the sound

 * @param volume

 */

play(sound: AudioClip | string, volume: number = 1.0) {

    if (sound instanceof AudioClip) {

        this._audioSource.stop();

        this._audioSource.clip = sound;

        this._audioSource.play();

        this.audioSource.volume = volume;

    }

    else {

        resources.load(sound, (err, clip: AudioClip) => {

            if (err) {

                console.log(err);

            }

            else {

                this._audioSource.stop();

                this._audioSource.clip = clip;

                this._audioSource.play();

                this.audioSource.volume = volume;

            }

        });

    }

}

/**

 * stop the audio play

 */

stop() {

    this._audioSource.stop();

}

/**

 * pause the audio play

 */

pause() {

    this._audioSource.pause();

}

/**

 * resume the audio play

 */

resume(){

    this._audioSource.play();

}

}

image 你得目录错了,resour要放在最外面的根目录不能内嵌

1赞

拷贝到根目录,也不行

你的resour目录截个图,属性也截个图看下

image

刚才重新清理了一下缓存,重开工程,现在可以了,感谢~~~