Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by NibblesMeKibbles · May 10, 2015 at 06:46 PM · c#audiosourceoffsetseconds

AudioSource play certain time range?

I have audio files containing several lines of dialogue each and I want to play just one phrase / line. Is it possible to play, for example, only 13.21 - 14.57 seconds into the AudioClip instantly?

AudioSource.time is get only, so I don't see any other options for setting the offset for which a AudioSource starts at.

I know how to wait and stop the sound, I just need to know how to play from an offset of seconds or samples. (C#)

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 Dibbie · May 10, 2015 at 08:33 PM 0
Share

Well, I never messed with that side of AudioSource before, but looking at the Unity references on it, these 3 functions might be some use maybe?

PlayScheduled Plays the clip at a specific time on the absolute time-line that AudioSettings.dspTime reads from.

SetScheduledEndTime Changes the time at which a sound that has already been scheduled to play will end. Notice that depending on the ti$$anonymous$$g not all rescheduling requests can be fulfilled.

SetScheduledStartTime Changes the time at which a sound that has already been scheduled to play will start.

Seems to look like what you want to do?

avatar image NibblesMeKibbles · May 10, 2015 at 08:50 PM 0
Share

All of those delay the Play. Not offset of the time at which the clip starts at. So if I PlayScheduled(13.21f) the game will wait for 13.21 seconds and then start from 0 seconds into the clip. What I want is to play immediately 13.21 seconds into the AudioClip, but I don't think it even is possible at this rate.

2 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by amanpu · Sep 11, 2015 at 10:28 AM

AudioSource.time is not get only, you can also set time using time property and seek starting time wherever you want.

If I understand your question properly then following should work :

 void Start()
 {
         audioSource.time = 13.21f;   // assuming that you already have reference to your AudioSource
         audioSource.Play();
 }
 
 void Update()
 {
         if(audioSource.time > 14.57f){
                 audioSource.Stop();
         }
 }

Comment
Add comment · 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
5

Answer by kalanadis · Dec 02, 2015 at 04:07 PM

@NibblesMeKibbles Try following 3 lines in order it works.

 audioSource.time = 13.21f
 audioSource.Play();
 audioSource.SetScheduledEndTime(AudioSettings.dspTime+(14.57f-13.21f));

Comment
Add comment · Show 1 · 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 GLeBaTi · Jun 22, 2018 at 08:38 AM 0
Share

Thanks!

  void PlaySoundInterval(float fromSeconds, toSeconds)
  {
     audioSource.time = fromSeconds;
     audioSource.Play();
     audioSource.SetScheduledEndTime(AudioSettings.dspTime+(toSeconds-fromSeconds));
  }


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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A* pathfinding - incorrect nodes calculated 1 Answer

How to make the camera follow and face a rolling object? 2 Answers

an object reference is required to access non static member problem 0 Answers


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