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 /
avatar image
0
Question by artfish · Oct 31, 2014 at 12:17 AM · timesoundstartwaveaudio clip

Audio Clip Random Start time, end random amount of timelater

Here's what I'm trying to do. I have 6 audio clips. I want to randomly load one and start it at a random time. I want it to stop a random amount of time later. This stop time would preferably be a float as I only want each clip to play between .4-.8 seconds.

Currently, I'm just trying to add 1 second to the stop time because I think that audio.timeSamples is an int and I don't really understand how to add a float amount. There's probably a better way to do it, but after a lot of Googling, I can't quite get it.

So far, it's not throwing any errors, but it's not doing what I want it to do. I just loads a random clip every frame. I think I'm supposed to be using the sample rate here somewhere, but I'm not understanding how to use it correctly. All of the clips are at 11025.

Thanks!

using UnityEngine; using System.Collections;

public class VoiceScanner : MonoBehaviour {

 public AudioClip[] myClip;
 int RandomStartTime = Random.Range(1,50);

 public AudioSource soundFile;
     // Use this for initialization

 void Start () {
         audio.clip = myClip[Random.Range(0,myClip.Length)];
         audio.Play();
         
     }
     
     void Update () {

     int startTimesample = RandomStartTime ;
     int endTimesample = RandomStartTime +(1);

     if (audio.timeSamples < startTimesample) {
         audio.timeSamples = startTimesample;
     } else if(audio.timeSamples > endTimesample){
         audio.Stop();
     }
 }
 

}

Comment
Add comment · Show 2
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 AlwaysSunny · Oct 30, 2014 at 10:41 PM 0
Share

I think you might want : http://docs.unity3d.com/ScriptReference/AudioSource-time.html

Incidentally, this sounds like a job for coroutines, to me. $$anonymous$$any time-interval-specific jobs are ideal candidates for coroutines : http://docs.unity3d.com/$$anonymous$$anual/Coroutines.html

avatar image artfish · Oct 31, 2014 at 02:10 AM 0
Share

Thanks AlwaysSunny. I've been playing around with corountines for the last couple of hours, new stuff for me. I think I have a decent grip on them now. Going to check those links and see what I can do.. Thanks

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Next enemy wave timer issue 0 Answers

Radial Sound Spectrum - Effect 0 Answers

Sound Timing 1 Answer

Trigger Start of Sound 1 Answer

Repeat Function 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