- Home /
The question is answered, right answer was accepted
Audio loop
So, I know how to loop a track, but is there any way to loop at a certain time?
Example: it should loop at "0:50 to 1:32"
Is that even possible with Unity? Please answer, thanks!
I would post the code, but I'm just using the Audio Listener and the Audio Source so please don't moan about the whole "MAKE ME A SCRIPT!" thing.
Answer by fafase · Jun 01, 2014 at 05:22 PM
You can use audio.time.
For instance this will start the clip at half of its length:
public AudioClip sound;
void Start(){
audio.clip = sound;
audio.time = audio.clip.length / 2f;
audio.Play();
}
Ok, but what is with the "/ 2f;"? I've never used audio.time... Sorry for bugging you a little bit more...
Actually, the time value is just the time of your audio in decimal, so ins$$anonymous$$d of 1.30 you have 90, the amount of seconds of the clips.
So if you know to start at 1:32 then 1 => 60 + 32 = 92
audio.time = 92f;
but this is what the docs says:
Be aware that: On a compressed audio track position does not necessary reflect the actual time in the track
Compressed audio is represented as a set of so-called packets.
The length of a packet depends on the compression settings and can quite often be 2-3 seconds per packet. See Also: timeSamples variable.
so yep, you need to try and maybe you will have to get away with almost what you wanted.
You set the time to the first value and you play until audio.time is equal to the second, then you stop playing.
You really have no clue what you are dealing with do you?
void StartClip(float timeStart, float timeEnd){
audio.clip = audioClip;
audio.time = timeStart;
audio.Play();
StartCoroutine(DelaySoundStop(timeEnd));
}
IEnumerator DelaySoundStop(float timeEnd)
{
while(audio.time < timeEnd)
yield return null;
audio.Stop();
}
Sorry to say but I will stop here. If someone wants to take over but I provided you with a solution but you seem not to be able to use it. That means you are ai$$anonymous$$g higher than you can handle and even though it could be a way to learn, you are just piling up questions until you get something that will work your way. This is no learning process.
Answer by kilian277 · Jun 01, 2014 at 09:16 PM
Here ya go
using UnityEngine;
using System.Collections;
public class PlayLengthLoop : MonoBehaviour {
public AudioClip audioClip;
void Start()
{
//Add your own times here !
StartClip(0.0f,0.0f);
}
void StartClip(float timeStart, float timeEnd)
{
audio.clip = audioClip;
audio.time = timeStart;
audio.Play();
StartCoroutine(DelaySoundStop(timeEnd));
}
IEnumerator DelaySoundStop(float timeEnd)
{
while(audio.time < timeEnd)
yield return null;
audio.Stop();
//Add your own times here !
StartClip(0.0f,0.0f);
}
}
$$anonymous$$ilian, this is a copy of my answer.
UA is not a place for generating code for others. You gave code, he will simply copy/paste and be back here next day to ask how to access the script from another script, this is what makes UA that bad, you and him.
Him for asking questions he cannot handle and you for spoonfeeding him so he knows that being back here he can just cry for a solution and you will give it.
It's better to have this than a constant spam$$anonymous$$g of duplicate questions clogging UA.
fafase, This is all I need, no need for more scripts at the moment, you're the one moaning right now.
I HAVE done my part of fixing the script and getting some help thanks to killian, so please, let's just leave it at that.
I actually have partial ad$$anonymous$$ rights due to my karma. You will also get it once you get to a certain amount. As for his code, it does not take a genius to compare the na$$anonymous$$g of variables and methods.
And I was not arguing anymore.
Answer by Blayer98 · Jun 01, 2014 at 08:21 PM
Ok, so I've finally fixed it.
I'm not sure about the audioClip part.
Assets/My Scripts/PlayLengthLoop.cs(6,18): error CS0103: The name `audioClip' does not exist in the current context
Not sure about this error.
using UnityEngine;
using System.Collections;
public class PlayLengthLoop : MonoBehaviour {
void StartClip(float timeStart, float timeEnd){
audio.clip = audioClip;
audio.time = timeStart;
audio.Play();
StartCoroutine(DelaySoundStop(timeEnd));
}
IEnumerator DelaySoundStop(float timeEnd)
{
while(audio.time < timeEnd)
yield return null;
audio.Stop();
}
}
You need to specify a new AudioClip clip; global variable.
Between these two lines:
public class PlayLengthLoop : $$anonymous$$onoBehaviour {
AudioClip clip;
void StartClip(float timeStart, float timeEnd){
here ya go
using UnityEngine;
using System.Collections;
public class PlayLengthLoop : $$anonymous$$onoBehaviour {
public AudioClip audioClip;
void Start()
{
//Add your own times here !
StartClip(0.0f,0.0f);
}
void StartClip(float timeStart, float timeEnd)
{
audio.clip = audioClip;
audio.time = timeStart;
audio.Play();
StartCoroutine(DelaySoundStop(timeEnd));
}
IEnumerator DelaySoundStop(float timeEnd)
{
while(audio.time < timeEnd)
yield return null;
audio.Stop();
//Add your own times here !
StartClip(0.0f,0.0f);
}
}
92f is the same as 92.0f , the f stand for floating point value. I put 0.0f there so yo ucan put your own times in place.
Follow this Question
Related Questions
How to make a timer read to the .001 of a second 2 Answers
How can i loop my animation clip? 2 Answers
Trigger audio loop on beat with PlayScheduled 1 Answer
When to create game music? After completion of game or before completion of game? 0 Answers
Unity and music tracks - load track at certain point and fading tracks in and out 1 Answer