- Home /
AudioSource problem.
Ok so I got this situation:
I play a sound when I hold the left mouse button then pause it when I release the left mouse button via this:
private AudioClip _useSound;
void Update() {
if(Input.GetMouseButton(0)) {
if(audio.clip != _useSound) audio.clip = _useSound;
if(!audio.IsPlaying) {
audio.Play();
}
}
if(Input.GetMouseButtonUp(0)) audio.Pause();
}
My problem:
It totally works. The playing, the pause if released then if I hold again it resumes. But whenever I disable the object that contains that code snippet, and re-enable to play the sound again it plays at the start of the sound and not to where it paused.
Here are the solutions I've tried before asking this question:
- Using the UnPause()
It works if the object's not disabled, but when disabled and re-enabled then play it just returns silence. Meaning no sound at all.
- Only changing the clip if the clip isn't the same as the intended clip
So I thought that the sound stops whenever I change clips so I did that but still the same problem.
Suspicions:
I'm suspecting that Unity is stopping AudioSources upon gameObject disable. Anyways to make it not be stopped if that's the case?
Any other possible solutions to this? =(
Answer by OncaLupe · Nov 07, 2015 at 07:13 PM
Looks like this is what you're looking for: http://docs.unity3d.com/ScriptReference/AudioSource-time.html
You should be able to get and set the position of the playback.
OnDisable()
{
pausedTime = audio.time;
}
OnEnable()
{
audio.time = pausedTime;
}
Yep perhaps this is it. Didn't notice that in the documentation. Thanks alot.
Answer by Sophistic_WaTer · Nov 09, 2015 at 05:44 AM
Another solution I've tried is to just save that time where the AudioSource paused then play it again on that time, but the problem I've had is that there's no function that does that like maybe PlayAtClipTime(float time);