- Home /
When an unity game on android is not focused and tries to call android api to play sound, the game will crash.
The question is about unity, android, and playing sound when the game is not focused. When the game is not focused and tries to call android API to play sound, the game will crash.
Background: We are building are an android game by unity and c#. The game is similar with werewolf. Players could speak one by one in a room. One player speaks and the rest of people in the room could hear.
Requirement: When a player stays in a game, the player could switch to other applications in android. Meanwhile the player shall still hear the speak from other players. For example, there are three players in the speaking queue of a room. Player one is speaking. Player two and three are waiting. At this moment, player three switches to browser in android to check news. Player three shall still hear player one. After player one finishes speaking and player two starts, player three shall still hear player two. When it is the turn for player three, he shall come back to the game and speak.
Challenge we have: In android system, once an application is not focused, the main thread of the application will be put to sleep. Although we successfully started a sub thread to receive protocols and sound data from game server when the game is not focused, yet the game crashes once we try to play the received sound data. We believe the reason that sound data will be played only by calling main thread. Calling main thread when the game is not focused will cause the crash.
Question: Is it possible for us to beat the challenge? And how to make it? Is there any event which can be triggered when the player comes back to game from using other application?