Browse Source

Merge pull request #360 from FungusGames/play-music-multiple-calls

Play Music call doesn't restart if same music already playing
master
Chris Gregan 9 years ago
parent
commit
e7fa98d3dd
  1. 5
      Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs
  2. 14
      Assets/Fungus/Audio/Scripts/MusicController.cs
  3. 246
      Assets/Tests/Audio/AudioTests.unity

5
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.")] [Tooltip("Time to begin playing in seconds. If the audio file is compressed, the time index may be inaccurate.")]
public float atTime; public float atTime;
[Tooltip("The music will start playing again at end.")]
public bool loop = true;
public override void OnEnter() public override void OnEnter()
{ {
MusicController musicController = MusicController.GetInstance(); MusicController musicController = MusicController.GetInstance();
if (musicController != null) if (musicController != null)
{ {
float startTime = Mathf.Max (0, atTime); float startTime = Mathf.Max (0, atTime);
musicController.PlayMusic(musicClip, startTime); musicController.PlayMusic(musicClip, loop, startTime);
} }
Continue(); Continue();

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

@ -41,11 +41,17 @@ namespace Fungus
* @param musicClip The music clip to play * @param musicClip The music clip to play
* @param atTime Time in the music clip to start at * @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<AudioSource>().clip = musicClip; AudioSource audioSource = GetComponent<AudioSource>();
GetComponent<AudioSource>().time = atTime; // May be inaccurate if the audio source is compressed http://docs.unity3d.com/ScriptReference/AudioSource-time.html BK if (audioSource != null &&
GetComponent<AudioSource>().Play(); 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();
}
} }
/** /**

246
Assets/Tests/Audio/AudioTests.unity

@ -461,7 +461,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ea6e8f632db87477eb750446b28d73a3, type: 3} m_Script: {fileID: 11500000, guid: ea6e8f632db87477eb750446b28d73a3, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 0 itemId: 9
errorMessage: errorMessage:
indentLevel: 0 indentLevel: 0
commenterName: commenterName:
@ -581,6 +581,196 @@ MonoBehaviour:
checkMethods: 1 checkMethods: 1
m_ActionBase: {fileID: 706075662} m_ActionBase: {fileID: 706075662}
checksPerformed: 0 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 --- !u!1 &641282520
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -719,7 +909,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!114 &741990853 --- !u!114 &741990853
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -798,7 +988,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3} m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
selectedFlowchart: {fileID: 1370059605} selectedFlowchart: {fileID: 637449677}
--- !u!4 &845793761 --- !u!4 &845793761
Transform: Transform:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1
@ -1495,6 +1685,56 @@ Transform:
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 5 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 --- !u!1 &2109416471
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

Loading…
Cancel
Save