Playmaker Forum

Bug Reporting => PlayMaker Bug Reporting => Topic started by: dasbin on July 06, 2015, 05:36:48 PM

Title: Audio Play doesn't Finish()
Post by: dasbin on July 06, 2015, 05:36:48 PM
Found a bug in the Audio Play action.
My states were all getting hung up on the action unless I was using the Finished Event feature of the action (which only fires the event when the audio is done playing... usually not desirable behavior, sometimes I want something else to happen right after starting a sound). My usual work-around was to manually send a delayed event right before using the action.

Looking at the code of the action, it will never call Finish() unless it either fails to play anything, or alongside calling the Finished Event selected. Normally, it will return out of all functions instead before Finish() is called.
If you don't specify a Finished Event it will hang on the action forever, and if you do it will only finish when the audio is done playing. Definitely a bug and not predictable behaviour.
Title: Re: Audio Play doesn't Finish()
Post by: jeanfabre on July 16, 2015, 09:04:23 AM
Hi,

 in that case, you should make an transition "DONE" or "NEXT" on that state and manually fire this event using send event.

but I see what you mean, can I rephrase?

you would like the Finish transition to be called if no finishedEvent was set on that action, correct?

Bye,

 Jean
Title: Re: Audio Play doesn't Finish()
Post by: dasbin on July 19, 2015, 03:25:21 PM
Hi Jean,

Yes, that's correct. Don't you think that's most consistent with all other actions in Playmaker? They Finish immediately unless some other event is specified to be called.
I've modified the action to do this, but it seems to me pretty likely that other users would find this perplexing as well.

Code: [Select]

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Audio)]
[Tooltip("Plays the Audio Clip set with Set Audio Clip or in the Audio Source inspector on a Game Object. Optionally plays a one shot Audio Clip.")]
public class AudioPlay : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(AudioSource))]
[Tooltip("The GameObject with an AudioSource component.")]
public FsmOwnerDefault gameObject;

[HasFloatSlider(0,1)]
        [Tooltip("Set the volume.")]
public FsmFloat volume;

[ObjectType(typeof(AudioClip))]
[Tooltip("Optionally play a 'one shot' AudioClip. NOTE: Volume cannot be adjusted while playing a 'one shot' AudioClip.")]
public FsmObject oneShotClip;

[Tooltip("Event to send when the AudioClip finishes playing.")]
public FsmEvent finishedEvent;

private AudioSource audio;

public override void Reset()
{
gameObject = null;
volume = 1f;
oneShotClip = null;
    finishedEvent = null;
}

public override void OnEnter()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go != null)
{
// cache the AudioSource component

    audio = go.GetComponent<AudioSource>();
if (audio != null)
{
var audioClip = oneShotClip.Value as AudioClip;

if (audioClip == null)
{
audio.Play();

if (!volume.IsNone)
{
audio.volume = volume.Value;
}

if (finishedEvent == null)
{
Finish ();
}


return;

}

if (!volume.IsNone)
{
audio.PlayOneShot(audioClip, volume.Value);
if (finishedEvent == null)
{
Finish ();
}
}
else
{
audio.PlayOneShot(audioClip);
if (finishedEvent == null)
{
Finish ();
}
}

return;
}
}

// Finish if failed to play sound

Finish();
}

public override void OnUpdate ()
{
if (audio == null)
{
Finish();
}
else
{
if (!audio.isPlaying)
{
Fsm.Event(finishedEvent);
Finish();
}
                else if (!volume.IsNone && volume.Value != audio.volume)
{
audio.volume = volume.Value;
}
}
}
}
}
Title: Re: Audio Play doesn't Finish()
Post by: jeanfabre on July 20, 2015, 06:50:16 AM
Hi,

 I agree. I will forward that to Alex, maybe it will be included in the next update. else we'll make it available on the ecosystem.

Bye,

 Jean
Title: Re: Audio Play doesn't Finish()
Post by: igud71 on September 27, 2016, 10:30:55 AM
Thank you Dasbin. You helped me.
And I totally agree with what you say.