0
\$\begingroup\$

I have wanted to make something like a rhythm game for Android in Unity, and for that I needed to write an audio manager to play my songs and sound effects in-game. I have followed Brackeys' tutorial for that, and it worked fine for me, even with sounds up to 5 minutes long.

Now I have built the game for Android, but if I press a button to play this song, it does not work at all any more. Sometimes the build just crashes, sometimes nothing happens, and most of the time the whole app freezes for about 5 to 10 seconds, and then the sound plays.

Also, in a Windows build this does not happen, and in-editor it works perfectly fine as well.

I have tried setting the audio to Best Latency, I have reimported all assets multiple times, and nothing worked.

Any help is highly appreciated because this is my first project! ( Also I'm not so good in English so sorry for that :) )

Update:

LevelManager.cs

using UnityEngine;
using System;
public class LevelManager : MonoBehaviour
{
    public Level[] levels;
    
    void Awake()
    {
        foreach (Level s in levels)
        {
            s.source = gameObject.AddComponent<AudioSource>();
            s.source.clip = s.clip;
            s.length = s.clip.length;
        }
    }

    public void Play(string name)
    {
        Level s = Array.Find(levels, sound => sound.name == name);
        if (s == null)
        {
            Debug.LogWarning("Sound: " +  name + " not found!");
            return;
        }
        s.source.Play();
    }
}

Level Class

using UnityEngine;

[System.Serializable]
public class Level
{
    [Header("General")]
    public string name;
    [HideInInspector] public float length;
    
    [Header("Audio")]
    public AudioClip clip;
    [HideInInspector] public AudioSource source;
}

This is the Level class in inspector

i think the import settings for one of the sounds

This is the import setting for one of the sounds.

\$\endgroup\$
5
  • \$\begingroup\$ Welcome to GDSE, and to game design! :) What kind of device are you testing your Android build on? Is it a real device or an emulator? Have you tested more than one device yet? \$\endgroup\$
    – Holo
    Mar 25 at 10:08
  • 1
    \$\begingroup\$ Can you show this sound asset's import settings, and how you're loading/playing it in code? It could be Unity hasn't loaded the sound until the moment it's about to play, causing a hitch as it loads the audio. If you can give Unity a heads up to start streaming the asset earlier, you can ensure it's in memory and ready to play at a moment's notice. The exact way to do this can differ based on your use case though, so it would help to see your code / audio source setup in addition to the info about the asset. \$\endgroup\$
    – DMGregory
    Mar 25 at 11:13
  • 1
    \$\begingroup\$ @holo, Samsung Galaxy A40, no emulator. I have tested 3 different phones and one tablet, it happens to all of them... \$\endgroup\$
    – CheckerT
    Mar 25 at 14:58
  • \$\begingroup\$ We're still missing the import settings for one of these clips. \$\endgroup\$
    – DMGregory
    Mar 25 at 18:09
  • \$\begingroup\$ @dmgregory i have added the import settings for one of the songs. \$\endgroup\$
    – CheckerT
    Mar 26 at 18:11

0

You must log in to answer this question.

Browse other questions tagged .