- Home /
Some sounds fail to play on Android
About half of my audio files are failing to play on Android. They do work on windows and in the Unity Environment. I'm playing all of the audio the same way--something along the lines of:
go = new GameObject();
go.AddComponent<AudioSource>();
go.audio.clip = Resources.Load(somePath);
go.audio.volume = 1;
go.audio.Play();
The files are all coming from the same location.
They are all marked as 2D.
All of the failing sounds are .WAV, though I do have working .WAV as well.
**All of the failing .WAV are 89kbps bit rate. The working .WAVs are 352kbps.
They are all marked as Form: Native (WAV) - Load Type: Load into memory.
If I set them to Audio Format: Compressed (MPEG) they DO work, but I'd like to know why.
Thanks!
Answer by RyanZimmerman87 · Apr 05, 2014 at 01:05 AM
I'm not an expert with Unity sounds, far from it but I have used them a lot without any significant problems like you are describing.
My first suggestion would be to just play the sounds differently. I'm not sure why you are creating a new Game Object and adding an Audio Source and then loading a clip from the resources every time you play a sound. That seems pretty excessive and I'm not sure why you would do it like that at run time. I'm assuming your Android device is having trouble doing all that fast enough for some of your files.
89KBPS is a very strange bitrate too. I've never seen that personally myself. Generally 128kbps seems to be the sweet spot for me when it comes to size vs quality. 352KBPS seems way too big especially for the way you are doing this on Android.
So I'm not sure how you are setting up these sounds like what objects they are attached too. But I think it's a good idea to have one script attached to your camera that plays all the general game-play sounds/music. And then all your separate objects like consumables or enemies can have their own sounds on their script.
But I do it a lot differently than how you set it up, I'm not 100% sure but it seems like the way you are doing it is not optimal for slower devices.
I would recommend trying something like this to play your sounds:
//declare sound variables publicly
//can set them up super easy this way
//no need to create a new game object
//no need to create new audio source
//no need to load sounds so many times
//That advice is not factual just what I am assuming
//so you declare audio clip variable publicly
//this Game Object should already have 1 Audio Source
public AudioClip usePotionSound;
//when you want to play audio
//just call a function to play sound
//you already have gameobject, audioclip, and audiosource ready to go
public void usePotionSoundFunction()
{
audio.PlayOneShot(usePotionSound);
}
//If you want more control
//you can do something like this
//to change settings on audio source
//for different clips
public AudioClip punchSound;
public AudioSource playerSkillsAudioSource;
public void playerPunchSoundFunction()
{
playerSkillsAudioSource.clip = punchSound;
playerSkillsAudioSource.minDistance = 10;
playerSkillsAudioSource.Play();
}
So I'm not 100% sure if this way is better than what you are doing. But it seems like it would be a lot better for mobile devices especially if you have all the sounds ready to go and loaded into memory instead of having to create a new game object and create new audio source and then load the resource.
You should also look into your bitrates both 89KBPS and 352KBPS seem a bit odd to me.
If these are short sound clips my understanding is that you should use .wav. For longer sounds like music that's when you wanna use the compressed.
If you try my examples and it still doesn't work maybe your Android device is just super slow or is running out of memory?
Unfortunately I'm running some of these sounds as you suggest and am still experiencing the issue. I do plan to refactor the discussed approach in the near future, thanks for your feedback there.
As for the bitrates, these are audio files I stole from Ultima Online to use as place holders. I will keep your recommendations in $$anonymous$$d when I generate my sounds. Right now I'm just trying to better understand how unity works (or doesn't in some cases). Overall, lots of good info, I will up vote as soon as Unity gives me enough reputation to. Thanks Ryan!
What kind of Android device are you using? $$anonymous$$aybe you are just running out of memory or can't load the sounds fast enough?
Nexus 5, Nexus 7, and Galaxy S3. All have the exact same behavior.
Your answer
Follow this Question
Related Questions
Save sound in unity to ringtone ( Android ) 1 Answer
Android Sound Delay 0 Answers
How to turn off sound on Android Device? 1 Answer
Android Sound Crackling. 1 Answer
Sound Vibration 1 Answer