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
0
Question by Stoyanow · Jun 25, 2015 at 01:03 PM · audiosliderlength

UI Slider Audio lenght

Hello, I am making an app for android, and I'm trying to set the value of the slider according to the time of the audio clip. I can move the slider, which forwards or rewinds to moments of the audio clip, but i can not make it move on it's own without any bugs. When i press play, the audio starts, it moves the slider, but it stutters, like i can hear it 2-3 times at once. I think it's because the slider.value is set to be modified in an update function. IDK guys, any help is greatly appriciated. Here's the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 public class AudioSlider : MonoBehaviour {
     
     public Slider audioSlider;
 
 
     
     void Start () {
 
         audio.Play();
 
         audioSlider.direction = Slider.Direction.LeftToRight;
         audioSlider.minValue = 0;
         audioSlider.maxValue = audio.clip.length;
 
     }
     
     
     void Update () {
 
         audioSlider.value = audio.time;    
       
     }
 }

P.S Nothing changes when "Play on awake" on the audiosource is checked/unchecked.

Comment
Add comment · Show 1
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 Nerevar · Jun 25, 2015 at 01:23 PM 0
Share

I have a solution for that, I'll share it once I get home.

here is a demo of the result. ( I dunno if the webplayer will work for you as it is now restricted by google, the app is slow to load).

I used the unity slider, grab the yellow bug to navigate in the song.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Nerevar · Jun 26, 2015 at 07:41 AM

Hello,

So I actually use this solution :

 public Slider TimeLine;
 // Flag to know if we are draging the Timeline handle
 private bool TimeLineOnDrag = false;
 
 void Update(){
 
     if (TimeLineOnDrag) {
 
         audio.timeSamples = (int)(audio.clip.samples * TimeLine.value);
 
     }
 
 
     } else {
             TimeLine.value = (float)audio.timeSamples / (float)clip audio.clip.samples;
 
     }
 
 }
 
 
 
 // Called by the event trigger when the drag begin
 public void TimeLineOnBeginDrag(){
 
     TimeLineOnDrag = true;
 
     audio.Pause();
 
     
 }
 
 
 // Called at the end of the drag of the TimeLine
 public void TimeLineOnEndDrag(){
 
     audio.Play();
 
     TimeLineOnDrag = false;
 }


I pause the audio while I move the handle that's better for the user's ears. Put the script where you have your AudioSource, reference the slider and link the last 2 function to the event trigger (for example OnMouseDown and OnMouseUp).

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
1

Answer by M_Kumar · Jan 19 at 07:50 AM

 IEnumerator Start()
 {
     slider.maxValue = audioSource.clip.length;
     slider.value = audioSource.time;
     while (audioSource.isPlaying)
     {
         slider.value = audioSource.time;
         UnityEngine.Debug.LogError("VALUE IS "+(audioSource.clip.length/audioSource.time));
         yield return null;
     }
     yield return null;
 }

You can then attach this function ( OnSliderValueChanged) to the Slider OnValueChanged event.

 public void OnSliderValueChanged ()
 {
     audioSource.time = slider.value;
 }
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
0

Answer by Dalerjonjon · Sep 14, 2020 at 05:18 PM

alt text

it showing error


ui-slider-audio-lenght-unity-answers-opera-2020-09.png (10.9 kB)
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to adjust game sound from UI Slider? 1 Answer

Compression slider in audio import settings doesn't seem to affect anything. 1 Answer

Vertical Slider - Using different art? 1 Answer

AudioListener.volume doesn't change the volume 4 Answers

Linear volume control of AudioMixer groups 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