Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

control audio playback position with slider

Discussion in 'Audio & Video' started by drudiverse, Mar 22, 2016.

  1. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Hi all, i have just tried for an hour to get a slider which scrolls forwards as a track progresses, and that can set the audio playback to a different position if i click forwards/backwards on the slider.

    It seems that the AudioSource.time does not update when instantaniously when you give a different AudioSource.time in the update function, so that you can try and write a new playback to audiosource.time and then set the scrollbar to the new audiosource.time, for example if the previous slider position and current slider position is larger than 44100 samples, i would tell the audiosource a new time and and it would be fine. didn't figure out how to do that in any way, it was very difficult. I tried many codes for example:

    Code (csharp):
    1.  
    2. function Update(){
    3.         if (isPlaying == true &&  Mathf.Abs(guioffset - offset) > 44100)
    4.             aud.timesamples = guioffset;
    5.  
    6.  
    7.         if (isPlaying == true && Mathf.Abs(guioffset - offset) <= 44100)
    8.             offset = aud.timeSamples;
    9.  

    so i tried to do it with 2 sliders. One that scolls automatically with audio playpos, which works, and one that can adjust audio play pos, which i haven't so far figured out.
    Code (csharp):
    1.  
    2.         if (isPlaying == true && prevguioffset == guioffset )    {
    3.             offset = aud.timeSamples/crushplay;  
    4.             }            
    5.         if (isPlaying == true && prevguioffset != guioffset ) {
    6.             print(guioffset + "  TIME   " + offset);
    7.             aud.timeSamples = guioffset * crushplay;
    8.             offset = guioffset;
    9.             prevguioffset = guioffset;
    10.             print(guioffset + "  TIME2   " + offset);}
    11.  
     
    Last edited: Mar 22, 2016
  2. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    Strange goings on with this task!!! DONT UNDERSTAND AT ALL!! SOME GENIUS PLEASE EXPLAIN THIS SIMPLE SAMPLE TIME RELATED UPDATE GUI BUG???... If i do this:

    if i make the slider adjust to 1/2 of it's new value in a singlue update, it zooms to the intended audio position pretty nicely
    Code (csharp):
    1.  
    2.         if(Input.GetMouseButton(0) && Input.mousePosition.y > Screen.height - 100)
    3.         {
    4.             offset          +=  (((Input.mousePosition.x/Screen.width) * list.Length / 3 ) -offset)/2 ; //div 1/2 at the end
    5.             aud.timeSamples +=  (((Input.mousePosition.x/Screen.width) * list.Length / 3 ) -offset)/2 ;  
    6.         }
    7.  
    If i make the audio adjust to 0.9 of the slider value instead of 0.5, the audio just changes position for one frame rate and then jumps back to the place it was playing at.

    it's a partial solution anyways, += 0.5 is a useable audio pos scrollbar, and timesamples = intended position bounces back to previous play after slider mouse is released.
     
  3. bayerly

    bayerly

    Joined:
    Apr 9, 2013
    Posts:
    25
    I'd recommend submitting a bug report to Unity with a reproducible case and they'll be able to determine if it is indeed a bug, or a logic error.
     
  4. bayerly

    bayerly

    Joined:
    Apr 9, 2013
    Posts:
    25
    Hmmm actually looking at it, try using and "else if" for the second if statement - I've had issues before where a bit of code above changes a conditional, then the code immediately after it becomes true.


    Something like:

    if (isPlaying == true && Mathf.Abs(guioffset - offset) > 44100) {
    aud.timesamples = guioffset;
    }


    else if (isPlaying == true && Mathf.Abs(guioffset - offset) <= 44100) {
    offset = aud.timeSamples;
    }