No announcement yet.

Detect when a User the Leaves the App to Fix a Backgournd Music MP3 issue

  • Filter
  • Time
  • Show
Clear All
new posts

    Detect when a User the Leaves the App to Fix a Backgournd Music MP3 issue

    I'm having a annoying issue with background music for my game which involves the music stopping when a user leaves the iOS app. The music will not resume in most cases when returning back to the app. (Using the home button, then going to another app, then returning creates this result)

    I've tried every method of playing mp3 background music that I know of:

    -Using Kismet's Play Music Track
    -Using "mobile PlaySong" console command
    -Using the "UpdateMusicTrack" function.

    It all creates the same results.

    While the control of playing mp3's is limited with UDK (since its iOS's own special hardware handling it) I think it would be much more easier to find a way to detect when the User returns to the app. That way when that occurs, I can simply play the music track again. Not the most elegant solution, but better than its current state.

    Btw, Ive tried using a Timer to play the music every few seconds to see what would happen when a user returns.
    It still yields the same results of silence. This occurs When using "mobile PlaySong"/"Kismet".

    However, when using "mobile PlaySong" it will loop the song every few seconds even after returning. This is important, because if I could find when the user returns to the app I can simply play a silence.mp3 then play the correct level track again.

    So far I've tried to use the following to detect when a user leaves/returns to an app:

    "event ClearPause()" in game info.


    "OnLostFocusPause()" in hud.

    None of these events are fired at all when returning to app in iOS. I can't seem to see any obvious kismet solutions either, though of course I welcome a kismet solution.

    Im using the latest Feb 2015 UDK (Since apple has new requirements I'm forced to use this, the previous UDK did not have this problem with background music)

    and Im using an iPad2 with the latest firmware (I think it's 9.1)

    I'm aware i've posted similar questions in other sections of the epicgames forums. But this post presents the entire problem and everything Ive tried so i'm sorry if it appears as spam.

    Update. I have found a solution. It's not super graceful, but it works and does exactly what I need.

    There exists a float var called "SessionPauseThresholdSec" in the AnalyticEventsBase class. Though I'm not exactly sure
    what this class is meant to be used for (uploading analytical data?) , we do know that "SessionPauseThresholdSec" is the minimum amount of time
    allowed to pass before a "session" is considered to be over. (As we see in the comments above it. Just look for it in AnalyticEventsBase.uc)

    This is an iOS specific float var that is used when an app is "backgrounded". If an app is "backgrounded" for more than the "SessionPauseThresholdSec"
    than that session is over.

    By reducing SessionPauseThresholdSec to a very small amount (I just used 0.001f here) then when returning after that small amount of time, it will be known that the session was over.
    Since the threshold is so small the session should always be over whenever the user exits the app using the home button/sleep button and returns to it.

    (if a user just presses the home button, looks at other apps, but never changes app the session will NOT end. But for my uses which is for fixing an mp3 issue its fine because that case did not result in the music stopping)

    So, I created a simple class with a timer which always check if the session has ever ended. If it has ended, then when the user returns, the timer will be aware because there is a IsSessionInProgress() that can be called and it will return false at that point. Then any function can be called etc. (in my case, play music again)

    Once the session has been detected to end, the AnalyticEventsBase object's session will have be restarted by calling "StartSession()" again.

    But yes. I could not find anyone else who has found a solution using UDK. I just made this class rather quickly and it an probably be cleaned up a bit.

    class PX1_ActiveAppManager extends Actor;
    var AnalyticEventsBase Analytics;
    function PostBeginPlay()
    	 Analytics = class'PlatformInterfaceBase'.static.GetAnalyticEventsInterface();
    	 Analytics.SessionPauseThresholdSec = 0.001f;
    	 setTimer(0.1f, true, 'checkActive');
    function checkActive()
    	if (Analytics != None)
    		 if (!Analytics.IsSessionInProgress())
    			WorldInfo.Game.Broadcast(self, "WAS INACTIVE!!!!");		
    function alertAppHasResumed()
    	WorldInfo.Game.Broadcast(self, "RETURNED FROM INACTIVE STATE");		
            //Do Whatever Code that needs to be done
    	//Need to know active status return even when paused
    	bAlwaysTick = true;

    For those interested in the music issue:

    If you call "mobile PauseSong" then "mobile ResumeSong" after a user returns to the app, then the background mp3 will resume where it last started. "mobile PauseSong" is needed otherwise it will not play at all for some reason.

    There is an interesting left over strange occurrence when using "mobile PauseSong". When trying to replay that same song from the beginning it will "resume" from where it last ended. For some reason, even when calling "mobile StopSong" it appears ios or udk still contains that pause data.

    A solution would be rather than calling PauseSong, to play a short silence.mp3 then play the desired song again. But this results in the song being restarted when returning to the app.

    I prefer the PauseSong method. Also, after the first "resume bug", it will then "restart" the song over again properly after that.

    It's a bit confusing, but I've had to do a lot of trial and error and this is what appears to be the case.

    Anyways, hope this helps anyone that needs it.