Skip to content

Commit a150caf

Browse files
Fix for issue #325
1 parent dc353ce commit a150caf

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

cocos2d/denshion/CCMusicPlayer.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,22 @@ public int SoundID
6363

6464
public void SaveMediaState()
6565
{
66-
// User is playing a song, so remember the song state.
67-
m_SongToPlayAfterClose = MediaPlayer.Queue.ActiveSong;
68-
m_VolumeAfterClose = MediaPlayer.Volume;
66+
try
67+
{
68+
// User is playing a song, so remember the song state.
69+
m_SongToPlayAfterClose = MediaPlayer.Queue.ActiveSong;
70+
m_VolumeAfterClose = MediaPlayer.Volume;
6971
#if !NETFX_CORE
70-
m_PlayPositionAfterClose = MediaPlayer.PlayPosition;
72+
m_PlayPositionAfterClose = MediaPlayer.PlayPosition;
7173
#endif
72-
m_IsRepeatingAfterClose = MediaPlayer.IsRepeating;
73-
m_IsShuffleAfterClose = MediaPlayer.IsShuffled;
74+
m_IsRepeatingAfterClose = MediaPlayer.IsRepeating;
75+
m_IsShuffleAfterClose = MediaPlayer.IsShuffled;
76+
}
77+
catch (Exception ex)
78+
{
79+
CCLog.Log("Failed to save the media state of the game.");
80+
CCLog.Log(ex.ToString());
81+
}
7482
}
7583

7684
public void RestoreMediaState()
@@ -84,8 +92,10 @@ public void RestoreMediaState()
8492
MediaPlayer.Volume = m_VolumeAfterClose;
8593
MediaPlayer.Play(m_SongToPlayAfterClose);
8694
}
87-
catch (Exception)
95+
catch (Exception ex)
8896
{
97+
CCLog.Log("Failed to restore the media state of the game.");
98+
CCLog.Log(ex.ToString());
8999
}
90100
}
91101
}

cocos2d/platform/CCApplication.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public CCApplication(Game game, IGraphicsDeviceService service)
5858

5959
WindowSetup = pp;
6060
Content = game.Content;
61+
HandleMediaStateAutomatically = true;
6162

6263
if (m_graphicsService.GraphicsDevice != null)
6364
{
@@ -143,12 +144,17 @@ public CCApplication(Game game, IGraphicsDeviceService service)
143144

144145
}
145146

147+
protected bool HandleMediaStateAutomatically { get; set; }
148+
146149
private void GameActivated(object sender, EventArgs e)
147150
{
148151
// Clear out the prior gamepad state because we don't want it anymore.
149152
m_PriorGamePadState.Clear();
150153
#if !IOS
151-
CocosDenshion.CCSimpleAudioEngine.SharedEngine.SaveMediaState();
154+
if (HandleMediaStateAutomatically)
155+
{
156+
CocosDenshion.CCSimpleAudioEngine.SharedEngine.SaveMediaState();
157+
}
152158
#endif
153159
ApplicationWillEnterForeground();
154160
}
@@ -157,7 +163,10 @@ private void GameDeactivated(object sender, EventArgs e)
157163
{
158164
ApplicationDidEnterBackground();
159165
#if !IOS
160-
CocosDenshion.CCSimpleAudioEngine.SharedEngine.RestoreMediaState();
166+
if (HandleMediaStateAutomatically)
167+
{
168+
CocosDenshion.CCSimpleAudioEngine.SharedEngine.RestoreMediaState();
169+
}
161170
#endif
162171
}
163172

tests/tests/classes/AppDelegate.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public AppDelegate(Game game, GraphicsDeviceManager graphics)
1313
s_pSharedApplication = this;
1414
CCDrawManager.InitializeDisplay(game, graphics, DisplayOrientation.LandscapeRight | DisplayOrientation.LandscapeLeft);
1515

16+
#if WINDOWS_PHONE8
17+
HandleMediaStateAutomatically = false; // Bug in MonoGame - https://github.com/Cocos2DXNA/cocos2d-xna/issues/325
18+
#endif
1619
game.Window.AllowUserResizing = true;
1720
graphics.PreferMultiSampling = false;
1821
#if WINDOWS || WINDOWSGL || WINDOWSDX || MACOS

0 commit comments

Comments
 (0)