- Home /
Music is not playing after build to APK
I have tested my game in the Editor and everything seems to be working fine. But after I build it into APK, the music won't load and the game got stuck on the main game scene. I believe there's something to do with my Main.cs script and some others. Note: I'm trying to load the audio on runtime because I'm using a music selection system.
Main.cs
public int CurrentBeat = 0;
public float BPM;
public bool RespawnNote = true;
public Scoring ScoreSystem;
public MusicData MusicData;
public int Difficulty = 1;
public int Speed = 4;
private float FallTime;
public AudioSource Music;
private AudioClip music2Play;
public GameHandler GameData;
void Awake(){
try
{
#region FromStart
// preparing objects
GameData = FindObjectOfType<GameHandler>(); // if this fails, go to catch line
music2Play = FindObjectOfType<AudioClip>();
// preparing data
MusicData = GameData.GetCurrentMusic();
FallTime = 16f / GameData.GameSpeed;
Difficulty = GameData.Difficulty;
Speed = GameData.GameSpeed;
isTest = false;
LoadAudio();
// set the clip to the AudioSource
Music.clip = music2Play;
#endregion
}
catch (System.Exception)
{
#region TestTrack
// this will be a test script
// so you don't need to start
// all the way from the beginning
// PS: I've set a track as default, so
// no more FileName on MusicData
isTest = true;
MusicData = new MusicData() { Title = "TestTrack", Artist = "<unknown/>",
BPM = 120, Offset = testOffset, TrackBeats = TestTrackBeats };
FallTime = 16f / Speed;
#endregion
}
finally
{
//these lines will be executed no matter what
ScoreSystem = FindObjectOfType<Scoring>();
cache = FindObjectOfType<ObjectPool>();
// preparing latency
lateStart = MusicData.Offset > FallTime;
}
}
void LoadAudio()
{
// load the audio file
// note: ignore extension
music2Play = Resources.Load<AudioClip>(GameData.RawPath + MusicData.FileNameNoExt);
music2Play.LoadAudioData();
}
IEnumerator Play()
{
if (!lateStart)
yield return new WaitForSeconds(FallTime + MusicData.Offset);
Music.Play();
}
I used Start() to call the coroutine Play()
Please, I need a quick solution to this issue. I need to get it finished in a week or two.
Answer by Komayo · Aug 11, 2017 at 01:44 AM
Iam not entirely a expert on sound matter, but this behaviour you are getting,
Seems a wrong format to play on android. Mobile systems are sometimes very strict and specific to some extensions/plugins types. Same on videos also.
Check also a few options of the file, compressions, bitrates, etc... a sound expert may give you a help in that
In the worst case, your android system runs out of resources to load the file, or not loading it quick enought to be readable by unity engine
Maybe this helps somehow.
I have tried a small project to load my resources on the fly. And all the audio files are loaded and worked perfectly. Even on AP$$anonymous$$.
$$anonymous$$y audio settings (for this one) are:
320kbps $$anonymous$$P3 file
Vorbis compression, 40% quality
Preload Audio Data = false
Load in Background = true
Load Type = Decompress on load
Again, I use Resources.Load<AudioClip>(path); to load the audio.
UPDATE: It's still not working. After implementing a log writer, turns out it's IsolatedStorageException. Android thinks that my resources is inside the AP$$anonymous$$, but there was nothing (I checked the built AP$$anonymous$$, and there was "res" ins$$anonymous$$d of "Resources").
In the given link, the errors were for text files (txt, json, xml). But I believe this could also happen on my audio files too. Or maybe anything that is inside the "Resources" folder.
There was a issue a few releases ago, with decompressed audio not working very well. Can you try to change Load Type to Compressed/Not Decompress and test again if it works.
Answer by MaxQuinonesSantander · Jun 16, 2021 at 06:19 AM
try change build to 64x in other settings =) works for me.