- Home /
Audio play and stop help (Kindle Fire)
public void PlayVoiceP1() { index = index*(-1); if(index == 1) audio.PlayOneShot(voiceP1); if(index == -1) audio.Stop(); }I made this function to make user can tap on an object to play audio and then tap again to stop. Everything works fin on iPad2. But after I switched to Kindle Fire, this way can not work. I tried several hours and found only this line of code can work: AudioSource.PlayClipAtPoint(voiceP1, camera.transform.position);
Anyone know why is that? or how to make it stop if I am using AudioSource.PlayClipAtPoint() ?
Answer by aldonaletto · Apr 11, 2012 at 04:40 PM
You could try audio.Play():
if (index == 1){ audio.clip = voiceP1; audio.Play(); }If this still doesn't work, you may have problems with the AudioSource attached to the object - Play() and PlayOneShot() use this audio source. Try to remove and recreate it - maybe some weird configuration is bugging the Android audio system.
WORKAROUND:
If nothing works, let's get the tricky way: PlayClipAtPoint creates a game object with an AudioSource and plays the sound, destroying the object when the sound finishes. The function doesn't return any reference to this object, but you can find it with GameObject.Find (at least in my PC Unity, this object is named "One shot audio"). Having its audio reference, you can stop and play the sound while it's alive. This isn't the best alternative, but may help (see a better one in the NOTES):
AudioSource myAudio;
public void PlayVoiceP1(){ index = -index; if (index == 1){ // it's time to play the sound: if (!myAudio){ // if myAudio doesn't exist... // create it and fire the sound: AudioSource.PlayClipAtPoint(voiceP1, transform.position); myAudio = GameObject.Find("One shot audio").audio; // then find it! } else { // if myAudio still alive, use it: myAudio.PlayOneShot(voiceP1); } } else { // if you must stop the sound, use myAudio if (myAudio) myAudio.Stop(); } } NOTES:
1- You could simplify things using a bool variable (play, for instance) instead of the numeric index (unless you really need a numeric variable for other purposes).
2- There's a smarter workaround: you can create the AudioSource the first time it's needed and play the sound. In subsequent occasions, you just use the AudioSource already created:
bool play = false; AudioSource myAudio;
public void PlayVoiceP1(){ if (!myAudio){ // if audio source doesn't exist yet, create it: GameObject go = new GameObject("MyAudio"); myAudio = go.AddComponent< AudioSource>(); } play = !play; // toggle the variable if (play){ // if became true... myAudio.PlayOneShot(voiceP1); // play the sound } else { // if false... myAudio.Stop(); // stop it } }
Answer by zombiewalker · Apr 11, 2012 at 06:29 PM
Problem solved. audio.PlayOneShot(); works totally fine. Turns out, it was a touchphase.moved issue The touch(swipe) was setting up too sensitive to cause audio stopped. (because I made it stop when swipe gesture happened)
Answer by zombiewalker · Apr 11, 2012 at 06:45 PM
Thanks Aldo! I found the problem came from my other line of code. Still appreciate your help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613070204im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Kindle Fire sound files always delayed 0 Answers
Activating Audio When Hitting a collider 2 Answers
2 audiolisteners in 1 scene 0 Answers
Play music troughout whole game in js 1 Answer
Screenshot Movie with Audio Capture 1 Answer