Author Topic: Scroll bar help  (Read 2556 times)

Adeel

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Scroll bar help
« on: April 14, 2014, 04:04:45 AM »
Hi guys,

I'm just after a bit of help with making a music scroll bar with ngui I have a 16 minute audio track and I am using a scroll bar to try and move it to the right point in the audio. I have it working in that it moves along with the audio however every time I move the scroll bar it always restarts the track from the beginning.
Heres the stuff I have so far:
  1. public UIScrollBar scrollBar;
  2.  
  3.         public AudioSource mainMusic;
  4.  
  5.         public float newMusicTime;
  6.  
  7.         void Update()
  8.         {
  9.                 if(AudioControllers.musicPlaying == true && AudioControllers.pauseMusic == false)
  10.                 {
  11.                         scrollBar.value += 1 / mainMusic.clip.length * Time.deltaTime;
  12.  
  13.                         if(Input.GetMouseButtonUp(0))
  14.                         {
  15.                                 mainMusic.time = scrollBar.value;
  16.  
  17.                         }
  18.                 }
  19.         }

I feel that I am close but its just not working any help would be much appreciated.

Thanks.

rain

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 79
    • View Profile
Re: Scroll bar help
« Reply #1 on: April 15, 2014, 08:16:41 AM »
If i'm not mistaken, the Scroll Bar's value only has a range from 0 to 1, so you're constantly setting the time on the audio source to something like the first 0-1 seconds.

If my above statement is right, then it should be something like this:
  1. public UIScrollBar scrollBar;
  2.  
  3.     public AudioSource mainMusic;
  4.  
  5.     public float newMusicTime;
  6.  
  7.     void Update()
  8.     {
  9.         if(AudioControllers.musicPlaying == true && AudioControllers.pauseMusic == false)
  10.         {
  11.             scrollBar.value += 1 / mainMusic.clip.length * Time.deltaTime;
  12.  
  13.             if(Input.GetMouseButtonUp(0))
  14.             {
  15.                 // so for example your scrollbar is at the half -> its .value is 0.5f
  16.                 // then you'd set the time to 0.5 * CLIPLENGTH
  17.                 mainMusic.time = scrollBar.value * mainMusic.clip.length;
  18.             }
  19.         }
  20.     }
  21.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scroll bar help
« Reply #2 on: April 15, 2014, 09:51:36 AM »
Yes that's right, scroll bar's value is always 0 to 1.

Adeel

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Scroll bar help
« Reply #3 on: April 16, 2014, 01:51:16 AM »
Thanks for the reply and sorry for the lateness of mine I've not had  a good Internet connection for the last couple of days!

Yeah the scroll bar value is 0-1 your suggestions looks about right I'll give it a go and reply when I've tested it on Thursday. Thanks again.

Adeel

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Scroll bar help
« Reply #4 on: April 17, 2014, 03:34:22 AM »
Thanks for your help rain just tested it and it works!