Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 02, 2014 at 07:30 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by Blayer98 · Jun 01, 2014 at 05:12 PM · gametimeloopmusic

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image
2
Best Answer

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();
 }
Comment
Add comment · Show 18 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blayer98 · Jun 01, 2014 at 05:23 PM 0
Share

Ok, but what is with the "/ 2f;"? I've never used audio.time... Sorry for bugging you a little bit more...

avatar image fafase · Jun 01, 2014 at 05:41 PM 1
Share

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.

avatar image fafase · Jun 01, 2014 at 06:42 PM 1
Share

You set the time to the first value and you play until audio.time is equal to the second, then you stop playing.

avatar image fafase · Jun 01, 2014 at 06:53 PM 1
Share

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();
 }
avatar image fafase · Jun 01, 2014 at 07:13 PM 1
Share

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.

Show more comments
avatar image
2

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);
     }
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blayer98 · Jun 01, 2014 at 09:19 PM 0
Share

Thanks! :)

avatar image fafase · Jun 02, 2014 at 06:46 AM 1
Share

$$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.

avatar image kilian277 · Jun 02, 2014 at 10:32 AM 1
Share

It's better to have this than a constant spam$$anonymous$$g of duplicate questions clogging UA.

avatar image Blayer98 · Jun 02, 2014 at 07:22 PM 0
Share

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.

avatar image fafase · Jun 02, 2014 at 07:48 PM 1
Share

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.

Show more comments
avatar image
0

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();
 }
 }
Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kilian277 · Jun 01, 2014 at 08:25 PM 1
Share

You need to specify a new AudioClip clip; global variable.

avatar image Blayer98 · Jun 01, 2014 at 08:34 PM 0
Share

Where would it fit onto my code?

avatar image kilian277 · Jun 01, 2014 at 08:38 PM 0
Share

Between these two lines:

public class PlayLengthLoop : $$anonymous$$onoBehaviour {

 AudioClip clip;

 void StartClip(float timeStart, float timeEnd){
avatar image kilian277 · Jun 01, 2014 at 09:05 PM 1
Share

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);
     }
 }
avatar image kilian277 · Jun 01, 2014 at 09:12 PM 1
Share

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.

Show more comments

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges