Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 cc.AudioClip 和 cc.AudioSource 以及内部的 AudioPlayer 的设计问题. #16995

Open
finscn opened this issue May 15, 2024 · 3 comments
Assignees
Labels
Bug Needs Triage Needs to be assigned by the team

Comments

@finscn
Copy link
Contributor

finscn commented May 15, 2024

Cocos Creator version

3.8.3

System information

all

Issue description

目前 cocos的设计是 在 cc.AudioClip 内创建时 , 其内部就创建了 一个 AudiPlayer 对象.

这个 AudiPlayer 对象在不同平台 实现方式是不一样的. 在 web 对应的是 webaudio(或者 Audio dom) ,
在 微信小游戏平台 对应的是 一个 InnerAudioContext 对象.

这个设计就产生了一个问题: 当开发者的本意只是 加载这些 音频资源 (暂时不播放) 时, 也会创建大量的 AudioPlayer.
我目前做的项目 一共 2000多个mp3文件. 虽然肯定不会同时播放. 但是为了游戏过程中的体验, 免不了要做异步懒加载/预加载.

这就导致 大量的 AudioPlayer 被创建 , 于是收到了微信平台的警告:

Warning(wx.createInnerAudioContext): Too many (200) InnerAudioContext instances have been created, 
    please destroy the existing instances first !

AudioClip 作为这一种资源(Asset), 最重要的是保存 资源文件的数据 (比如一个 buffer ), 通常无需创建独立的 AudioPlayer.
而且 cocos 的设计里, AudioClip本身也不具备自己播放的能力. 通常都是要挂接到一个 cc.AudioSource 上.

所以 这个地方建议优化一下设计. AudioClip 内部不要再包含 player了.
当 clip 被赋值到 cc.AudioSource.clip 上时, 在AudioSource上创建就好, 而且我看代码, 现在 AudioSource 内部本来也是要创建的.

而且我和一些开发者交流下来, 其实大家 都会在实际项目中选择 用少量的 AudioSource 来播放大量的 clip.
所以 player 放到 AudioSource 里, clip不再持有player 应该是一种更合理的设计.

Relevant error log output

No response

Steps to reproduce

Minimal reproduction project

No response

@finscn finscn added Bug Needs Triage Needs to be assigned by the team labels May 15, 2024
@minggo
Copy link
Contributor

minggo commented May 15, 2024

虽然我对这个模块不是很了解,但是从上面的分析来看,还是很合理的建议。

@finscn
Copy link
Contributor Author

finscn commented May 15, 2024

另外 AudioClip 上有 setVolume setLoop 方法.
AudioSource 上也有, 还能设置的不一样.

A

clip.setVolum(0.8)
source.volum = 0.3
source.clip = clip
source.play()

B

source.volum = 0.3
clip.setVolum(0.8)
source.clip = clip
source.play()

C

source.clip = clip
clip.setVolum(0.8)
source.volum = 0.3
source.play()

D

source.clip = clip
source.volum = 0.3
clip.setVolum(0.8)
source.play()

ABCD 播放的实际音量是多少?

并不是 后设置的优先, 也不是 clip优先 或者 source优先....
实际情况取决于 source.clip = clip 的时机, 有点乱...

@PPpro
Copy link
Contributor

PPpro commented May 16, 2024

AudioClip 本来就是不应该包含 player 的,这个 player 是为了兼容历史版本留下的包袱

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Needs to be assigned by the team
Projects
None yet
Development

No branches or pull requests

4 participants