playMaker

Author Topic: Get Current Audio Source Playback Time, Pause Audio, Restart from Saved Point?  (Read 441 times)

westingtyler

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Is there a way to do this in playmaker? I want to be able to play audio tapes in my game, pause them, and restart them from the saved point, and optionally set a loop point, like for a song with a start but no end.


tcmeric

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
On the ecosystem there is "audio get time" action.

I am not sure about playing an audio clip from a specific time. If no one has suggestions for a specific action for this, I could probably write one for you.

There is an "audio pause" action.

Unity can loop the whole track out of the box (there is also an audio loop actions which can turn this off and on). But it cannot loop from a specific time.

tcmeric

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
Yikes. So I wrote an audio clip looper using a coroutine, but seems it cannot be stopped. Literally. So I will have to rethink this  :o

Otherwise it works great, lol.

Code: [Select]
// Custom Action by DumbGameDev
// www.dumbgamedev.com

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Audio)]
[Tooltip("Sets looping on the AudioSource component for a specific time range.")]
public class SetAudioLoopTime : ComponentAction<AudioSource>
{
[RequiredField]
[CheckForComponent(typeof(AudioSource))]
public FsmOwnerDefault gameObject;
public FsmFloat startTime;
public FsmFloat endTime;
public FsmBool loopAudio;

public override void Reset()
{
gameObject = null;
loopAudio = false;
startTime = null;
endTime = null;
}

public override void OnStart()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (UpdateCache(go))

{

StartCoroutine (playClipLength (loopAudio.Value));

}

}


IEnumerator playClipLength(bool playback)
{

do {
audio.time = startTime.Value;
audio.Play ();
audio.SetScheduledEndTime (AudioSettings.dspTime + (endTime.Value - startTime.Value));
yield return new WaitForSeconds (endTime.Value - startTime.Value);
Debug.Log ("Waited" + playback);
} while(playback);

}
}

}

tcmeric

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
I think I got it  8) Ill make a little tutorial to go with it.

You can set the start time of the clip to play, as well as the end time. Takes float values. It has a finish event that only fires once the clip is finished (so it doesnt stop the clip too early). In my tutorial ill show how to loop it .

Code: [Select]
// Custom Action by DumbGameDev
// www.dumbgamedev.com

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Audio)]
[Tooltip("Sets looping on the AudioSource component for a specific time range.")]
public class SetAudioLoopTime : ComponentAction<AudioSource>
{
[RequiredField]
[CheckForComponent(typeof(AudioSource))]
public FsmOwnerDefault gameObject;
public FsmFloat startTime;
public FsmFloat endTime;
public FsmEvent finishEvent;

private float timer;
private float trackLength;


public override void Reset()
{
gameObject = null;
startTime = null;
endTime = null;
finishEvent = null;
}

public override void OnEnter()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (UpdateCache(go))

{
//set variables for onEnter
timer = 0f;
trackLength = endTime.Value - startTime.Value;


//Play Audio Clip with Start and End Time
playClipLength ();


// End event if audio clip length is less than 0
if (trackLength <= 0)
{
Fsm.Event(finishEvent);
Finish();
return;
}

}

}

public override void OnUpdate()
{

// Set a timer
timer += Time.deltaTime;


// Timer vs Track length to trigger end event
if (timer >= trackLength)
{
Finish();
if (finishEvent != null)
{
Fsm.Event(finishEvent);
}
}

}

void playClipLength()
{

// Play audio clip for certain time values
audio.time = startTime.Value;
audio.Play ();
audio.SetScheduledEndTime (AudioSettings.dspTime + (endTime.Value - startTime.Value));

}
}

}

« Last Edit: April 06, 2017, 05:54:11 AM by tcmeric »

tcmeric

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
Ok, wrote two actions.

1. SetAudioStartTime
(Set a specific audio start time using a FSM float, and then plays audio).

2. SetAudioLoopTime
(Set a specific start and end play time to an audio clip, and then plays audio. Has finish event for looping).

You can download my actions here: https://github.com/dumbgamedev/general-playmaker/tree/master/Audio

Here is a youtube tutorial on how to use these actions, how to setup looping with a bool, and how to pause and unpause audio using a key. https://www.youtube.com/watch?v=XaDtCVa2jIU

westingtyler

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Hey! this looks fantastic. thanks for the enthusiastic work. I haven't tried it yet, but if it works, that's amazing and opens many possibilities. And if it doesn't work, I am still grateful you tackled it!

tcmeric

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
No problem. If it doesnt work, please let me know, so I can make corrections  8)

HeathClose

  • Full Member
  • ***
  • Posts: 223
    • View Profile
thanks for spending the calories on us... I think I can use this... thanks again

HeathClose

  • Full Member
  • ***
  • Posts: 223
    • View Profile
are you using screenflow for you videos?

tcmeric

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
PC. I used to use camtasia and got fed-up. Crashed a lot and my audio always sounded bad.

 I changed to xsplit. Which I hadent considered for video tutorials before, but it works great. Love the ability to change the size and position of my webcam during screen capture.

Its a few dollars a month for the personal edition. It comes with a terrible video editor, but does the trick. Im too lazy to bring it into sony vegas or anything else these days to edit. Mic is the blue yeti. Popular mid priced high quality mic. (Excellent mic for the price for sure).

tcmeric

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
(And run it through handbrake before upload, as my internet connection in Asia is slow).