diff --git a/Demo/API/Assets/Scenes/InnerAudio.unity b/Demo/API/Assets/Scenes/InnerAudio.unity index c06c8c906f33e8f955e3b84517e036f511a87012..bf94bf1dbd546f96a95f7e472f373ef56fd09e78 100644 --- a/Demo/API/Assets/Scenes/InnerAudio.unity +++ b/Demo/API/Assets/Scenes/InnerAudio.unity @@ -1199,18 +1199,6 @@ MonoBehaviour: m_StringArgument: m_BoolArgument: 0 m_CallState: 2 - - m_Target: {fileID: 2105647632} - m_TargetAssemblyTypeName: AudioManager, Assembly-CSharp - m_MethodName: stopAllAudio - m_Mode: 1 - m_Arguments: - m_ObjectArgument: {fileID: 0} - m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine - m_IntArgument: 0 - m_FloatArgument: 0 - m_StringArgument: - m_BoolArgument: 0 - m_CallState: 2 --- !u!114 &1439564064 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Demo/API/Assets/Scripts/AudioManager.cs b/Demo/API/Assets/Scripts/AudioManager.cs index 3ad3806006f7466bbff9900e23b66e616309b5c7..54a38a18d323b04d95e16b139ef9621b5feb1ccb 100644 --- a/Demo/API/Assets/Scripts/AudioManager.cs +++ b/Demo/API/Assets/Scripts/AudioManager.cs @@ -9,10 +9,10 @@ public class AudioManager : MonoBehaviour private static int DEFAULT_AUDIO_COUNT = 10; // 创建音频队列 - private Queue audioPool = new Queue(); + private static Queue audioPool = new Queue(); // 当前场景需要预下载的音频列表 - private string[] audioList = { + private static string[] audioList = { "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20220901/211827/CallMeTeenTop.mp3", "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20220815/105451/1.mp3", "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20220901/211846/Night-n.mp3", @@ -21,7 +21,9 @@ public class AudioManager : MonoBehaviour }; // 正在播放的音频对象列表 - private List audioPlayArray = new List(); + private static List audioPlayArray = new List(); + + private bool isDestroyed = false; // 初始化 public void Start() @@ -44,6 +46,10 @@ public class AudioManager : MonoBehaviour private WXInnerAudioContext getAudio() { + if (this.isDestroyed) { + return null; + } + if (audioPool.Count == 0) { addAudio(); @@ -116,6 +122,10 @@ public class AudioManager : MonoBehaviour { var audioIndex = getAudio(); + if (audioIndex == null) { + return; + } + // 如果没有下文修改needDownload为false的函数,理论上创建的所有音频都是true,可以省去这一条 audioIndex.needDownload = true; @@ -137,6 +147,10 @@ public class AudioManager : MonoBehaviour // 但是再次使用该音频时会因为没有下载而需要再次下载,并不推荐这样使用 var audioPlayRightNow = getAudio(); + if (audioPlayRightNow == null) { + return; + } + // 修改src会触发下载,所以设置needDownload属性要在修改src之前 audioPlayRightNow.needDownload = false; @@ -152,7 +166,10 @@ public class AudioManager : MonoBehaviour public void stopAllAudio() { - audioPlayArray.ForEach(audio => audio.Stop()); + audioPlayArray.ForEach(audio => { + audio.OffCanplay(); + audio.Stop(); + }); } public void playRandom() @@ -163,4 +180,10 @@ public class AudioManager : MonoBehaviour playRightNow(index); } + + private void OnDestroy() + { + this.isDestroyed = true; + this.stopAllAudio(); + } } \ No newline at end of file diff --git a/Design/AudioOptimization.md b/Design/AudioOptimization.md index b1e96b7fc06288bef39cf1bbbce5d46526a074d8..f21a20f34ba5f85eb84f726fcad2f65bc363d3c5 100644 --- a/Design/AudioOptimization.md +++ b/Design/AudioOptimization.md @@ -13,9 +13,6 @@ 参考[微信开发者文档](https://developers.weixin.qq.com/minigame/dev/api/media/audio/InnerAudioContext.html) 其中src为音频地址,可填本地路径如 xx.wav,运行时会自动和配置的音频地址前缀(默认为DATA_CDN/Assets)做拼接得到最终线上地址。 -* 使用方法一: - - ```c# // 使用方法一:创建音频对象可以在初始化是加上needDownload = true,音频会先下载到本地,然后再播放 // 保存本地后,同样的路径不会重复下载,再次使用时无需下载 @@ -59,6 +56,10 @@ WX.PreDownloadAudios(a, (int res) => }); } }); + +// 停止音频播放 +audio.OffCanplay(); +audio.Stop(); ``` ## 参考示例