playMaker

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

westingtyler

  • Sr. Member
  • ****
  • Posts: 277
    • My Video Game Projects Web Site
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

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
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

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
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

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
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, 08:54:11 AM by tcmeric »

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
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.

westingtyler

  • Sr. Member
  • ****
  • Posts: 277
    • My Video Game Projects Web Site
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

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
No problem. If it doesnt work, please let me know, so I can make corrections  8)

HeathClose

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

HeathClose

  • Full Member
  • ***
  • Posts: 226
are you using screenflow for you videos?

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
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

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
(And run it through handbrake before upload, as my internet connection in Asia is slow).

SamH

  • Playmaker Newbie
  • *
  • Posts: 41
Thanks! SetAudioStartTime is exactly what I needed!