提交 123e4d6c 编写于 作者: U uhu

feat: 优化音频播放示例

上级 0a7ead88
......@@ -337,6 +337,140 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 594407489}
m_CullTransparentMesh: 0
--- !u!1 &647899803
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 647899804}
- component: {fileID: 647899807}
- component: {fileID: 647899806}
- component: {fileID: 647899805}
m_Layer: 5
m_Name: Resume
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &647899804
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647899803}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1611328529}
m_Father: {fileID: 2105647631}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: -270}
m_SizeDelta: {x: 320, y: 60}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &647899805
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647899803}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 647899806}
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 2105647632}
m_TargetAssemblyTypeName: AudioManager, Assembly-CSharp
m_MethodName: resumeAllAudio
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 4
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &647899806
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647899803}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &647899807
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 647899803}
m_CullTransparentMesh: 0
--- !u!1 &945804509
GameObject:
m_ObjectHideFlags: 0
......@@ -614,11 +748,11 @@ RectTransform:
m_Children:
- {fileID: 1026316877}
m_Father: {fileID: 2105647631}
m_RootOrder: 4
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: -270}
m_AnchoredPosition: {x: 0, y: -360}
m_SizeDelta: {x: 320, y: 60}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1187294105
......@@ -786,7 +920,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Play Long 5
m_Text: Pause All
--- !u!222 &1260414954
CanvasRenderer:
m_ObjectHideFlags: 0
......@@ -902,7 +1036,7 @@ GameObject:
- component: {fileID: 1302124278}
- component: {fileID: 1302124277}
m_Layer: 5
m_Name: Long 5
m_Name: Pause
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
......@@ -975,7 +1109,7 @@ MonoBehaviour:
m_Calls:
- m_Target: {fileID: 2105647632}
m_TargetAssemblyTypeName: AudioManager, Assembly-CSharp
m_MethodName: playLong5
m_MethodName: pauseAllAudio
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
......@@ -1094,7 +1228,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Play Long
m_Text: Play BGM
--- !u!222 &1360654997
CanvasRenderer:
m_ObjectHideFlags: 0
......@@ -1136,7 +1270,7 @@ RectTransform:
m_Children:
- {fileID: 2074957559}
m_Father: {fileID: 2105647631}
m_RootOrder: 5
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
......@@ -1304,6 +1438,86 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1611328528
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1611328529}
- component: {fileID: 1611328531}
- component: {fileID: 1611328530}
m_Layer: 5
m_Name: Text
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1611328529
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1611328528}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 647899804}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1611328530
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1611328528}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 32
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 3
m_MaxSize: 40
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Resume All
--- !u!222 &1611328531
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1611328528}
m_CullTransparentMesh: 0
--- !u!1 &1970663473
GameObject:
m_ObjectHideFlags: 0
......@@ -1451,7 +1665,7 @@ GameObject:
- component: {fileID: 2039791430}
- component: {fileID: 2039791429}
m_Layer: 5
m_Name: Play Long
m_Name: Play BGM
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
......@@ -1524,7 +1738,7 @@ MonoBehaviour:
m_Calls:
- m_Target: {fileID: 2105647632}
m_TargetAssemblyTypeName: AudioManager, Assembly-CSharp
m_MethodName: playLong
m_MethodName: playBGM
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
......@@ -1752,6 +1966,7 @@ RectTransform:
- {fileID: 2039791428}
- {fileID: 594407490}
- {fileID: 1302124276}
- {fileID: 647899804}
- {fileID: 1187294104}
- {fileID: 1439564062}
m_Father: {fileID: 0}
......
......@@ -6,7 +6,7 @@ using WeChatWASM;
public class AudioManager : MonoBehaviour
{
// cdn路径音频最多支持10个同时在线播放,先下载后的音频(needDownload)最多支持32个同时播放,先初始化10个
private static int DEFAULT_AUDIO_COUNT = 10;
//private static int DEFAULT_AUDIO_COUNT = 10;
// 创建音频队列
private static Queue<WXInnerAudioContext> audioPool = new Queue<WXInnerAudioContext>();
......@@ -30,6 +30,9 @@ public class AudioManager : MonoBehaviour
// 正在播放的音频对象列表
private static List<WXInnerAudioContext> audioPlayArray = new List<WXInnerAudioContext>();
// 背景音乐
private WXInnerAudioContext audioBGM = null;
private bool isDestroyed = false;
private int createdAudioCount = 0;
......@@ -37,16 +40,17 @@ public class AudioManager : MonoBehaviour
// 初始化
public void Start()
{
// 创建音频对象池,创建时设置属性需要下载
for (var i = 0; i < DEFAULT_AUDIO_COUNT; i++)
{
addAudio();
};
// // 创建音频对象池,创建时设置属性需要下载
// for (var i = 0; i < DEFAULT_AUDIO_COUNT; i++)
// {
// addAudio(false);
// };
// 批量下载音频文件
downloadAudio();
}
// 从缓存池中获取音频实例
private WXInnerAudioContext getAudio()
{
if (this.isDestroyed)
......@@ -56,26 +60,35 @@ public class AudioManager : MonoBehaviour
if (audioPool.Count == 0)
{
addAudio();
addAudio(false);
}
var audio = audioPool.Dequeue();
audio.needDownload = true;
audioPlayArray.Add(audio);
return audio;
}
private void removeAudio(WXInnerAudioContext audio)
// 销毁或回收实例
private void removeAudio(WXInnerAudioContext audio, bool needDestroy = true)
{
audio.OffCanplay();
if (audioPlayArray.Contains(audio))
{
audioPlayArray.Remove(audio);
}
if (!audioPool.Contains(audio))
if (needDestroy)
{
audioPool.Enqueue(audio);
audio.Destroy();
}
else
{
if (!audioPool.Contains(audio))
{
audioPool.Enqueue(audio);
}
}
Debug.Log("___________________");
......@@ -83,7 +96,9 @@ public class AudioManager : MonoBehaviour
Debug.Log("___________________");
}
private WXInnerAudioContext addAudio()
// 创建InnerAudioContext实例
// 参数needDestroy表示是否需要在播放完之后销毁,目前建议都先销毁再创建使用
private WXInnerAudioContext addAudio(bool needDestroy = true)
{
if (createdAudioCount > 32)
{
......@@ -98,7 +113,7 @@ public class AudioManager : MonoBehaviour
audio.OnEnded(() =>
{
Debug.Log(audio.instanceId + " OnEnded");
removeAudio(audio);
removeAudio(audio, needDestroy);
});
// 加载出错
......@@ -106,14 +121,14 @@ public class AudioManager : MonoBehaviour
{
Debug.Log(audio.instanceId + "audio OnError");
audio.Stop();
removeAudio(audio);
removeAudio(audio, needDestroy);
});
// 手动停止
audio.OnStop(() =>
{
Debug.Log(audio.instanceId + "audio OnStop");
removeAudio(audio);
removeAudio(audio, needDestroy);
});
// 暂停
......@@ -128,11 +143,15 @@ public class AudioManager : MonoBehaviour
Debug.Log(audio.instanceId + "audio OnPlay");
});
audioPool.Enqueue(audio);
if (!needDestroy)
{
audioPool.Enqueue(audio);
}
return audio;
}
// 预下载音频
private void downloadAudio()
{
// 预下载音频
......@@ -152,7 +171,8 @@ public class AudioManager : MonoBehaviour
});
}
public void playAfterDownload(int index, bool isEffect)
// 播放音频
public void playAfterDownload(int index, bool isShort)
{
var audioIndex = getAudio();
......@@ -161,9 +181,6 @@ public class AudioManager : MonoBehaviour
return;
}
// 如果没有下文修改needDownload为false的函数,理论上创建的所有音频都是true,可以省去这一条
audioIndex.needDownload = true;
// 如果要设置的src和原音频对象一致,可以直接播放
if (audioIndex.src == audioList[index])
{
......@@ -176,13 +193,14 @@ public class AudioManager : MonoBehaviour
// 如果该文件没有下载过,等同于先调用WX.PreDownloadAudios下载后再播放
audioIndex.src = audioList[index];
if (isEffect)
// 短音频可以直接调用Play
if (isShort)
{
audioIndex.Play();
}
else
{
// 在可以播放时播放
// 长音频在可以播放时播放
audioIndex.OnCanplay(() =>
{
audioIndex.Play();
......@@ -191,20 +209,20 @@ public class AudioManager : MonoBehaviour
}
}
// 不缓存立即播放
public void playRightNow(int index)
{
// 如果是需要在当前场景立刻播放的音频,则不设置needDownload,音频会边下边播
// 但是再次使用该音频时会因为没有下载而需要再次下载,并不推荐这样使用
var audioPlayRightNow = getAudio();
audioPlayRightNow.needDownload = false;
if (audioPlayRightNow == null)
{
return;
}
// 修改src会触发下载,所以设置needDownload属性要在修改src之前
audioPlayRightNow.needDownload = false;
// 如果要设置的src和原音频对象一致,可以直接播放
if (audioPlayRightNow.src == audioList[index])
{
......@@ -223,6 +241,26 @@ public class AudioManager : MonoBehaviour
}
}
// 暂停所有在播放的音乐
public void pauseAllAudio()
{
audioPlayArray.ForEach(audio =>
{
audio.Pause();
});
}
// 重新播放所有在播放的音乐
public void resumeAllAudio()
{
audioPlayArray.ForEach(audio =>
{
// innerAudio没有resume,直接用play重新播放
audio.Play();
});
}
// 停止所有在播放的音乐
public void stopAllAudio()
{
audioPlayArray.ForEach(audio =>
......@@ -232,23 +270,16 @@ public class AudioManager : MonoBehaviour
});
}
// 播放短音频
public void playShort()
{
var index = new System.Random().Next(0, 5);
Debug.Log("Play:" + index);
Debug.Log("playShort:" + index);
playAfterDownload(index, true);
}
public void playLong()
{
var index = new System.Random().Next(5, 10);
Debug.Log("Play:" + index);
playAfterDownload(index, false);
}
// 播放5个短音频
// 同时播放5个短音频(测试用)
public void playShort5()
{
for (var i = 0; i < 5; i++)
......@@ -257,15 +288,27 @@ public class AudioManager : MonoBehaviour
};
}
// 播放5个长音频
public void playLong5()
// 播放背景音乐
public void playBGM()
{
for (var i = 0; i < 5; i++)
var index = new System.Random().Next(5, 10);
Debug.Log("Play:" + index);
if (audioBGM != null)
{
this.playLong();
};
audioBGM.Stop();
}
// 长音频在使用后需要销毁
audioBGM = addAudio(true);
audioPlayArray.Add(audioBGM);
audioBGM.src = audioList[index];
audioBGM.OnCanplay(() =>
{
audioBGM.Play();
});
}
// 销毁场景
private void OnDestroy()
{
this.isDestroyed = true;
......
......@@ -38,7 +38,7 @@ MonoBehaviour:
loadingBarWidth: 240
needCheckUpdate: 0
SDKOptions:
UseAudioApi: 0
UseAudioApi: 1
UseFriendRelation: 0
UseCompressedTexture: 0
CompileOptions:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册