Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by zombiewalker · Apr 11, 2012 at 03:01 PM · audiokindle fire

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() ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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 } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges