1
0
mirror of https://github.com/thes3m/XNI synced 2024-12-26 13:26:06 +01:00

Game music now dynamically stops/starts during runtime if you turn on/off your own music.

git-svn-id: http://xni.googlecode.com/svn/XNI@128 ac433895-eea3-a490-d80a-17149a75e588
This commit is contained in:
Matej Jan 2012-11-26 16:24:56 +00:00
parent 705dc046c7
commit 4409443ae0
6 changed files with 68 additions and 0 deletions

View File

@ -12,12 +12,14 @@
#import "Retronator.Xni.Framework.h"
#import "Retronator.Xni.Framework.Graphics.h"
#import "Retronator.Xni.Framework.Content.h"
#import "Retronator.Xni.Framework.Media.h"
#import "TouchPanel+Internal.h"
#import "GameWindow+Internal.h"
#import "GameViewController.h"
#import "GameView.h"
#import "Guide+Internal.h"
#import "SoundEffect+Internal.h"
#import "MediaPlayer+Internal.h"
@interface Game ()
@ -233,6 +235,7 @@ static NSArray *drawOrderSort;
{
NSLog(@"Application was deactivated.");
isActive = NO;
[MediaPlayer toBackground];
[deactivated raiseWithSender:self];
}
@ -240,6 +243,7 @@ static NSArray *drawOrderSort;
{
NSLog(@"Application was activated.");
isActive = YES;
[MediaPlayer returnFromBackground];
[activated raiseWithSender:self];
}

View File

@ -21,6 +21,8 @@
MediaQueue *queue;
MediaState state;
float volume;
Song *songToPlayOnActive;
NSMutableArray *remainingSongIndices;

View File

@ -7,6 +7,7 @@
//
#import "MediaPlayer.h"
#import "MediaPlayer+Internal.h"
#import <AudioToolbox/AudioToolbox.h>
@ -20,6 +21,11 @@
- (void) setMediaState:(MediaState)value;
- (void) fillSongIndices;
- (void) setSongToPlayOnActive:(Song*)song;
- (void) toBackground;
- (void) returnFromBackground;
@end
@ -115,6 +121,8 @@ static MediaPlayer *instance;
+ (void) playSong:(Song*)song { [instance playSong:song];}
+ (void) resume { [instance resume];}
+ (void) stop { [instance stop];}
+ (void) toBackground { [instance toBackground];}
+ (void) returnFromBackground { [instance returnFromBackground];}
- (void) moveNext {
if (![self checkAvailability]) {
@ -158,6 +166,8 @@ static MediaPlayer *instance;
- (void) playSong:(Song*)song {
if (![self checkAvailability]) {
// Save the song if we might get availability later.
[self setSongToPlayOnActive:song];
return;
}
@ -244,11 +254,51 @@ static MediaPlayer *instance;
}
}
- (void)setSongToPlayOnActive:(Song *)song {
[songToPlayOnActive release];
songToPlayOnActive = [song retain];
}
- (void)toBackground {
// If music was playing, activate the ambient category while the app is in background.
if (soloModeActivated) {
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error:nil];
}
}
- (void)returnFromBackground {
// If music was playing, try to return to playing if we still have control.
// Otherwise the user has started playing his own music and we should remain in ambient.
if (soloModeActivated) {
if (self.gameHasControl) {
// Everything is OK, set category back to solo.
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategorySoloAmbient error:nil];
} else {
// Stop music if we lost control.
[self setSongToPlayOnActive:queue.activeSong];
queue.activeSong.audioPlayer.currentTime = 0;
[queue.activeSong.audioPlayer stop];
[self setMediaState:MediaStateStopped];
soloModeActivated = NO;
}
} else {
if (self.gameHasControl && songToPlayOnActive) {
[self playSong:songToPlayOnActive];
[self setSongToPlayOnActive:nil];
}
}
}
- (void) dealloc
{
[remainingSongIndices release];
[activeSongChanged release];
[mediaStateChanged release];
[songToPlayOnActive release];
[queue release];
[super dealloc];
}

View File

@ -0,0 +1,8 @@
@interface MediaPlayer (Internal)
+ (void) toBackground;
+ (void) returnFromBackground;
@end

View File

@ -664,6 +664,7 @@
B57E36BF124BE2DD00DDAA42 /* VertexPositionColorTexture.m in Sources */ = {isa = PBXBuildFile; fileRef = B57E36BD124BE2DD00DDAA42 /* VertexPositionColorTexture.m */; };
B57E36CD124BE36E00DDAA42 /* VertexPositionColorTextureArray.m in Sources */ = {isa = PBXBuildFile; fileRef = B57E36CB124BE36E00DDAA42 /* VertexPositionColorTextureArray.m */; };
B594878612AEF44900EE601F /* XniPoint.m in Sources */ = {isa = PBXBuildFile; fileRef = B594878412AEF44900EE601F /* XniPoint.m */; };
B5956CCA1663BD2700FC01F5 /* MediaPlayer+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = B5956CC91663BD2700FC01F5 /* MediaPlayer+Internal.h */; };
B59AD7F11236E07300F99511 /* ContentImporter.m in Sources */ = {isa = PBXBuildFile; fileRef = B59AD7EF1236E07300F99511 /* ContentImporter.m */; };
B59AD8121236E25700F99511 /* TextureImporter.m in Sources */ = {isa = PBXBuildFile; fileRef = B59AD8101236E25700F99511 /* TextureImporter.m */; };
B59AD8201236E59A00F99511 /* ContentItem.m in Sources */ = {isa = PBXBuildFile; fileRef = B59AD81E1236E59A00F99511 /* ContentItem.m */; };
@ -936,6 +937,7 @@
B594878112AEF3DA00EE601F /* PointStruct.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PointStruct.h; sourceTree = "<group>"; };
B594878312AEF44900EE601F /* XniPoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XniPoint.h; sourceTree = "<group>"; };
B594878412AEF44900EE601F /* XniPoint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XniPoint.m; sourceTree = "<group>"; };
B5956CC91663BD2700FC01F5 /* MediaPlayer+Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "MediaPlayer+Internal.h"; path = "Classes/Retronator/Xni/Framework/MediaPlayer+Internal.h"; sourceTree = SOURCE_ROOT; };
B59AD7EE1236E07300F99511 /* ContentImporter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContentImporter.h; sourceTree = "<group>"; };
B59AD7EF1236E07300F99511 /* ContentImporter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ContentImporter.m; sourceTree = "<group>"; };
B59AD80B1236E21900F99511 /* Retronator.Xni.Framework.Content.Pipeline.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Retronator.Xni.Framework.Content.Pipeline.h; sourceTree = "<group>"; };
@ -1260,6 +1262,7 @@
B507F85D12E5766C00A7302B /* Song+Internal.h */,
B507F85012E5756400A7302B /* Song.m */,
B507F83E12E5751700A7302B /* MediaPlayer.h */,
B5956CC91663BD2700FC01F5 /* MediaPlayer+Internal.h */,
B507F83F12E5751700A7302B /* MediaPlayer.m */,
B507F8BA12E58E7000A7302B /* MediaQueue.h */,
B507F8C412E58EB100A7302B /* MediaQueue+Internal.h */,
@ -2249,6 +2252,7 @@
B54E75561356241B007AD718 /* ReadOnlyCollection.h in Headers */,
B54E75571356241B007AD718 /* IAsyncResult.h in Headers */,
25AE5D5315F5440B00B73D10 /* RenderTarget2D.h in Headers */,
B5956CCA1663BD2700FC01F5 /* MediaPlayer+Internal.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};