From 101f04915df8b1f3d084341cdb86e70474f04a73 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Wed, 20 Jan 2016 16:32:59 +0000 Subject: [PATCH] Play Music call doesn't restart if same music already playing Add a loop property --- .../Audio/Scripts/Commands/PlayMusic.cs | 5 +- .../Fungus/Audio/Scripts/MusicController.cs | 14 +- Assets/Tests/Audio/AudioTests.unity | 246 +++++++++++++++++- 3 files changed, 257 insertions(+), 8 deletions(-) diff --git a/Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs b/Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs index 6a29c074..0f02343d 100644 --- a/Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs +++ b/Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs @@ -15,13 +15,16 @@ namespace Fungus [Tooltip("Time to begin playing in seconds. If the audio file is compressed, the time index may be inaccurate.")] public float atTime; + [Tooltip("The music will start playing again at end.")] + public bool loop = true; + public override void OnEnter() { MusicController musicController = MusicController.GetInstance(); if (musicController != null) { float startTime = Mathf.Max (0, atTime); - musicController.PlayMusic(musicClip, startTime); + musicController.PlayMusic(musicClip, loop, startTime); } Continue(); diff --git a/Assets/Fungus/Audio/Scripts/MusicController.cs b/Assets/Fungus/Audio/Scripts/MusicController.cs index a4a7682c..fcbca6d5 100644 --- a/Assets/Fungus/Audio/Scripts/MusicController.cs +++ b/Assets/Fungus/Audio/Scripts/MusicController.cs @@ -41,11 +41,17 @@ namespace Fungus * @param musicClip The music clip to play * @param atTime Time in the music clip to start at */ - public void PlayMusic(AudioClip musicClip, float atTime = 0) + public void PlayMusic(AudioClip musicClip, bool loop, float atTime = 0) { - GetComponent().clip = musicClip; - GetComponent().time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK - GetComponent().Play(); + AudioSource audioSource = GetComponent(); + if (audioSource != null && + audioSource.clip != musicClip) + { + 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(); + } } /** diff --git a/Assets/Tests/Audio/AudioTests.unity b/Assets/Tests/Audio/AudioTests.unity index 139475e4..dd7009b0 100644 --- a/Assets/Tests/Audio/AudioTests.unity +++ b/Assets/Tests/Audio/AudioTests.unity @@ -461,7 +461,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ea6e8f632db87477eb750446b28d73a3, type: 3} m_Name: m_EditorClassIdentifier: - itemId: 0 + itemId: 9 errorMessage: indentLevel: 0 commenterName: @@ -581,6 +581,196 @@ MonoBehaviour: checkMethods: 1 m_ActionBase: {fileID: 706075662} checksPerformed: 0 +--- !u!1 &637449670 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 142980, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, type: 2} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 637449671} + - 114: {fileID: 637449677} + - 114: {fileID: 637449676} + - 114: {fileID: 637449675} + - 114: {fileID: 637449674} + - 114: {fileID: 637449673} + - 114: {fileID: 637449672} + - 114: {fileID: 637449679} + - 114: {fileID: 637449678} + m_Layer: 0 + m_Name: Flowchart + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &637449671 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 467082, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + 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: 1979947549} + m_RootOrder: 0 +--- !u!114 &637449672 +MonoBehaviour: + m_ObjectHideFlags: 2 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1902400ccc99b45d69ad01cb86b57d0f, type: 3} + m_Name: + m_EditorClassIdentifier: + itemId: 3 + errorMessage: + indentLevel: 0 + musicClip: {fileID: 8300000, guid: 476f002f45c7655408b9e7b1fa186f26, type: 3} + atTime: 0 + loop: 0 +--- !u!114 &637449673 +MonoBehaviour: + m_ObjectHideFlags: 2 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3315ad2ebb85443909a1203d56d9344e, type: 3} + m_Name: + m_EditorClassIdentifier: + itemId: 2 + errorMessage: + indentLevel: 0 + duration: 0.5 +--- !u!114 &637449674 +MonoBehaviour: + m_ObjectHideFlags: 2 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + 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: 476f002f45c7655408b9e7b1fa186f26, type: 3} + atTime: 0 + loop: 0 +--- !u!114 &637449675 +MonoBehaviour: + m_ObjectHideFlags: 2 + m_PrefabParentObject: {fileID: 11462346, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d2f6487d21a03404cb21b245f0242e79, type: 3} + m_Name: + m_EditorClassIdentifier: + parentBlock: {fileID: 637449676} +--- !u!114 &637449676 +MonoBehaviour: + m_ObjectHideFlags: 2 + m_PrefabParentObject: {fileID: 11433304, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3d3d73aef2cfc4f51abf34ac00241f60, type: 3} + m_Name: + m_EditorClassIdentifier: + nodeRect: + serializedVersion: 2 + x: 68 + y: 70 + width: 120 + height: 40 + itemId: 0 + blockName: Start + description: + eventHandler: {fileID: 637449675} + commandList: + - {fileID: 637449674} + - {fileID: 637449673} + - {fileID: 637449672} + - {fileID: 637449678} + - {fileID: 637449679} +--- !u!114 &637449677 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 11430050, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + 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: 400 + zoom: 1 + scrollViewRect: + serializedVersion: 2 + x: -343 + y: -340 + width: 1114 + height: 859 + selectedBlock: {fileID: 0} + selectedCommands: [] + variables: [] + description: 'Manual test for consecutive Play Music commands playing the same clip. + + The sound should play without restarting on the second Play Music command.' + stepPause: 0 + colorCommands: 1 + hideComponents: 1 + saveSelection: 1 + localizationId: + hideCommands: [] +--- !u!114 &637449678 +MonoBehaviour: + m_ObjectHideFlags: 2 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + 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 &637449679 +MonoBehaviour: + m_ObjectHideFlags: 2 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 637449670} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4920f47cde1a84b11ad07b7317568494, type: 3} + m_Name: + m_EditorClassIdentifier: + itemId: 4 + errorMessage: + indentLevel: 0 --- !u!1 &641282520 GameObject: m_ObjectHideFlags: 0 @@ -719,7 +909,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!114 &741990853 MonoBehaviour: m_ObjectHideFlags: 0 @@ -798,7 +988,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3} m_Name: m_EditorClassIdentifier: - selectedFlowchart: {fileID: 1370059605} + selectedFlowchart: {fileID: 637449677} --- !u!4 &845793761 Transform: m_ObjectHideFlags: 1 @@ -1495,6 +1685,56 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_RootOrder: 5 +--- !u!1 &1979947547 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1979947549} + - 114: {fileID: 1979947548} + m_Layer: 0 + m_Name: PlayMusicAgainTest + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1979947548 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1979947547} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b1dba0b27b0864740a8720e920aa88c0, type: 3} + m_Name: + m_EditorClassIdentifier: + timeout: 5 + ignored: 0 + succeedAfterAllAssertionsAreExecuted: 0 + expectException: 0 + expectedExceptionList: + succeedWhenExceptionIsThrown: 0 + includedPlatforms: -1 + platformsToIgnore: [] + dynamic: 0 + dynamicTypeName: +--- !u!4 &1979947549 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1979947547} + 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: 637449671} + m_Father: {fileID: 0} + m_RootOrder: 7 --- !u!1 &2109416471 GameObject: m_ObjectHideFlags: 0