- Home /
Rewind button works but not fast forward button?
Hello, everyone. I'm doing a project for school on the connection of music to the brian, and I'm using Unity because of it's gui system. I need to be able to fast forward and rewind audio clips, and I'm doing so with audio.pitch
. However, the fast forward button has no effect, but the rewind button works fine. All help would be appreciated!!! :)
Here is my code:
@script RequireComponent(AudioSource)
private var INFOscrollVector : Vector2 = Vector2.zero; private var AUDIOscrollVector : Vector2 = Vector2.zero;
var info : String; var songs : AudioClip[]; var clipButtonSize : float; var PlayIcon : Texture2D; var PauseIcon : Texture2D; var ffIcon : Texture2D; var rwIcon : Texture2D; var showAudioInterface : boolean = true; private var aClip : AudioClip; private var p : float;
function Awake() { p = audio.pitch; } function OnGUI() { AudioInterface(); } function Update() { audio.clip = aClip; } function AudioInterface() { //Background Boxes GUI.Box(Rect(10, 10, Screen.width/2-5, Screen.height - 60), ""); GUI.Box(Rect(Screen.width/2+5, 10, Screen.width/2-10, Screen.height - 60), ""); GUI.Box(Rect(10, Screen.height-50, Screen.width - 15, 40), ""); //The Play/Pause and other options GUI.BeginGroup(Rect(10, Screen.height-50, Screen.width - 15, 40)); if(GUI.Button(Rect(0, 0, 40, 40), PlayIcon)) { audio.Play(); } if(GUI.Button(Rect(40, 0, 40, 40), PauseIcon)) { audio.Pause(); } if(GUI.RepeatButton(Rect(80, 0, 40, 40), ffIcon)) { audio.pitch = p * 2; } else { audio.pitch = p; } if(GUI.RepeatButton(Rect(120, 0, 40, 40), rwIcon)) { audio.pitch = p/2; } else { audio.pitch = p; } GUI.EndGroup();
//The Info Scroll View
INFOscrollVector = GUI.BeginScrollView(Rect(10, 10, Screen.width/2-5, Screen.height - 60), INFOscrollVector, Rect(10, 10, Screen.width/2-25, 10000));
GUI.Label(Rect(10, 10, Screen.width - 20, 10000), info);
GUI.EndScrollView();
//The Audio Scroll View
AUDIOscrollVector = GUI.BeginScrollView(Rect(Screen.width/2+5, 10, Screen.width/2-10, Screen.height - 60), AUDIOscrollVector, Rect(10, 10, Screen.width/2-30, clipButtonSize*songs.length));
for(i = 0; i < songs.length; i++)
{
if(GUI.Button(Rect(10, 10+(i*clipButtonSize), Screen.width/2-30, clipButtonSize), songs[i].name))
{
aClip = songs[i];
}
}
GUI.EndScrollView();
}
Answer by Richard J. Hansen · Apr 29, 2011 at 02:06 AM
I don't have any experience with playing around with pitch, but I think your problem is here.
if(GUI.RepeatButton(Rect(80, 0, 40, 40), ffIcon))
{
audio.pitch = p * 2;
} else
{
audio.pitch = p;
}
if(GUI.RepeatButton(Rect(120, 0, 40, 40), rwIcon))
{
audio.pitch = p/2;
} else
{
audio.pitch = p;
}
After you set the pitch faster for the ff button you then check the rewind button. However, since you aren't pressing the rewind button, it sets the pitch back to 'p.'
Something like this would be better.
audio.pitch = p;
if(GUI.RepeatButton(Rect(80, 0, 40, 40), ffIcon))
{
audio.pitch = p * 2;
}
if(GUI.RepeatButton(Rect(120, 0, 40, 40), rwIcon))
{
audio.pitch = p/2;
}
NOTE: in my preview window it does not look like the code tags are going to work for this post: not sure why. If it does come out ugly, I will come back to it and edit it with a different browser later.
Your answer
Follow this Question
Related Questions
Fast forward an audio clip 1 Answer
Animate Audio Source Pitch/Speed in Timeline 0 Answers
Is it possible to raise an audio sources pitch as an audio listener gets closer? 5 Answers
Checking collision between 2 triggers based on distance. Possible? 1 Answer
How do I get the fundamental frequency pitch for audio input? 0 Answers