Browse Source

Fixed Add auto cross fade to Play Music command #199

master
chrisgregan 9 years ago
parent
commit
96c0dddc2e
  1. 11
      Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs
  2. 27
      Assets/Fungus/Audio/Scripts/MusicController.cs
  3. 249
      Assets/Tests/Audio/AudioTests.unity

11
Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs

@ -17,19 +17,22 @@ namespace Fungus
[Tooltip("The music will start playing again at end.")]
public bool loop = true;
[Tooltip("Length of time to fade out previous playing music.")]
public float fadeDuration = 1f;
public override void OnEnter()
{
MusicController musicController = MusicController.GetInstance();
if (musicController != null)
{
float startTime = Mathf.Max (0, atTime);
musicController.PlayMusic(musicClip, loop, startTime);
float startTime = Mathf.Max(0, atTime);
musicController.PlayMusic(musicClip, loop, fadeDuration, startTime);
}
Continue();
}
public override string GetSummary()
{
if (musicClip == null)

27
Assets/Fungus/Audio/Scripts/MusicController.cs

@ -41,17 +41,38 @@ namespace Fungus
* @param musicClip The music clip to play
* @param atTime Time in the music clip to start at
*/
public void PlayMusic(AudioClip musicClip, bool loop, float atTime = 0)
public void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime)
{
AudioSource audioSource = GetComponent<AudioSource>();
if (audioSource != null &&
audioSource.clip != musicClip)
if (audioSource == null || audioSource.clip == musicClip)
{
return;
}
if (fadeDuration == 0f)
{
audioSource.clip = musicClip;
audioSource.loop = loop;
audioSource.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK
audioSource.Play();
}
else
{
float startVolume = audioSource.volume;
LeanTween.value(gameObject, startVolume, 0f, fadeDuration)
.setOnUpdate( (v) => {
// Fade out current music
audioSource.volume = v;
}).setOnComplete( () => {
// Play new music
audioSource.volume = startVolume;
audioSource.clip = musicClip;
audioSource.loop = loop;
audioSource.time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK
audioSource.Play();
});
}
}
/**

249
Assets/Tests/Audio/AudioTests.unity

@ -633,6 +633,7 @@ MonoBehaviour:
musicClip: {fileID: 8300000, guid: 476f002f45c7655408b9e7b1fa186f26, type: 3}
atTime: 0
loop: 0
fadeDuration: 1
--- !u!114 &637449673
MonoBehaviour:
m_ObjectHideFlags: 2
@ -665,6 +666,7 @@ MonoBehaviour:
musicClip: {fileID: 8300000, guid: 476f002f45c7655408b9e7b1fa186f26, type: 3}
atTime: 0
loop: 0
fadeDuration: 1
--- !u!114 &637449675
MonoBehaviour:
m_ObjectHideFlags: 2
@ -730,7 +732,7 @@ MonoBehaviour:
y: -340
width: 1114
height: 859
selectedBlock: {fileID: 0}
selectedBlock: {fileID: 637449676}
selectedCommands: []
variables: []
description: 'Manual test for consecutive Play Music commands playing the same clip.
@ -877,6 +879,197 @@ MonoBehaviour:
other: {fileID: 0}
otherPropertyPath:
constantValueGeneric: 0
--- !u!1 &677393342
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 142980, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, type: 2}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 677393343}
- 114: {fileID: 677393349}
- 114: {fileID: 677393348}
- 114: {fileID: 677393347}
- 114: {fileID: 677393346}
- 114: {fileID: 677393345}
- 114: {fileID: 677393344}
- 114: {fileID: 677393350}
- 114: {fileID: 677393351}
m_Layer: 0
m_Name: Flowchart
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &677393343
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 467082, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
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_Children: []
m_Father: {fileID: 1140384750}
m_RootOrder: 0
--- !u!114 &677393344
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3315ad2ebb85443909a1203d56d9344e, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 3
errorMessage:
indentLevel: 0
duration: 2
--- !u!114 &677393345
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1902400ccc99b45d69ad01cb86b57d0f, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 2
errorMessage:
indentLevel: 0
musicClip: {fileID: 8300000, guid: f3c866bbc4639e7409c30c71736d84a6, type: 3}
atTime: 0
loop: 1
fadeDuration: 1
--- !u!114 &677393346
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1902400ccc99b45d69ad01cb86b57d0f, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 1
errorMessage:
indentLevel: 0
musicClip: {fileID: 8300000, guid: 19f33067513db4e9cb3a36d5c984bba7, type: 3}
atTime: 0
loop: 1
fadeDuration: 1
--- !u!114 &677393347
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 11462346, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d2f6487d21a03404cb21b245f0242e79, type: 3}
m_Name:
m_EditorClassIdentifier:
parentBlock: {fileID: 677393348}
--- !u!114 &677393348
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 11433304, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3d3d73aef2cfc4f51abf34ac00241f60, type: 3}
m_Name:
m_EditorClassIdentifier:
nodeRect:
serializedVersion: 2
x: 70.13808
y: 70
width: 120
height: 40
itemId: 0
blockName: Start
description:
eventHandler: {fileID: 677393347}
commandList:
- {fileID: 677393346}
- {fileID: 677393351}
- {fileID: 677393345}
- {fileID: 677393344}
- {fileID: 677393350}
--- !u!114 &677393349
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11430050, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7a334fe2ffb574b3583ff3b18b4792d3, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 1.0
scrollPos: {x: 0, y: 0}
variablesScrollPos: {x: 0, y: 0}
variablesExpanded: 1
blockViewHeight: 301
zoom: 0.9559973
scrollViewRect:
serializedVersion: 2
x: -343
y: -340
width: 1114
height: 859
selectedBlock: {fileID: 677393348}
selectedCommands: []
variables: []
description: "Manual test to check if Play Music fades out existing music before
\nplaying a new music clip."
stepPause: 0
colorCommands: 1
hideComponents: 1
saveSelection: 1
localizationId:
hideCommands: []
--- !u!114 &677393350
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4920f47cde1a84b11ad07b7317568494, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 4
errorMessage:
indentLevel: 0
--- !u!114 &677393351
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 677393342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3315ad2ebb85443909a1203d56d9344e, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 5
errorMessage:
indentLevel: 0
duration: 2
--- !u!114 &706075662
MonoBehaviour:
m_ObjectHideFlags: 0
@ -988,7 +1181,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3}
m_Name:
m_EditorClassIdentifier:
selectedFlowchart: {fileID: 637449677}
selectedFlowchart: {fileID: 677393349}
--- !u!4 &845793761
Transform:
m_ObjectHideFlags: 1
@ -1001,6 +1194,56 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
--- !u!1 &1140384748
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1140384750}
- 114: {fileID: 1140384749}
m_Layer: 0
m_Name: FadeMusicTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!114 &1140384749
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1140384748}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b1dba0b27b0864740a8720e920aa88c0, type: 3}
m_Name:
m_EditorClassIdentifier:
timeout: 5
ignored: 0
succeedAfterAllAssertionsAreExecuted: 1
expectException: 0
expectedExceptionList:
succeedWhenExceptionIsThrown: 0
includedPlatforms: -1
platformsToIgnore: []
dynamic: 0
dynamicTypeName:
--- !u!4 &1140384750
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1140384748}
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_Children:
- {fileID: 677393343}
m_Father: {fileID: 0}
m_RootOrder: 8
--- !u!1 &1280611022
GameObject:
m_ObjectHideFlags: 0
@ -1700,7 +1943,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &1979947548
MonoBehaviour:
m_ObjectHideFlags: 0

Loading…
Cancel
Save