在cocos2d app中全局打开/关闭声音

时间:2012-07-04 05:20:57

标签: iphone ios cocos2d-iphone

我使用了单例类并在所有类中使用来打开/关闭应用程序声音。问题是当我改变屏幕时声音即使被静音也会打开。我在每个类的init中调用声音方法,因此它会打开。但是如果我不从init调用它就永远不会启动。我搞砸了。

以下是我使用的

//SingletonClass.m
+(SingletonClass*)sharedMySingleton
{
 .....
}
+(id)alloc{
 ......
}

-(id)init{
    self = [super init];
         if (self != nil) {
             // initialize stuff here
             is_sound_enable = TRUE;
    //         [[SimpleAudioEngine sharedEngine] setMute:NO];
             [[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
            }              
      return self;
}

-(void)setsound{
    if(is_sound_enable == TRUE){
//       [[SimpleAudioEngine sharedEngine] setMute:NO]; //this is not working when called again
        [[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
//        is_sound_enable = FALSE;
    }
    else{
//        [[SimpleAudioEngine sharedEngine] setMute:YES]; // this is not working when called again
        [[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
//        is_sound_enable = TRUE;
    }

    if(is_sound_enable == TRUE){
        is_sound_enable = FALSE;
    }
    else{
        is_sound_enable = TRUE;
    }
}


//MyClass.m
-(void)toggleSoound{
[[SingletonClass sharedMySingleton] setsound];
}

1 个答案:

答案 0 :(得分:4)

按如下方式创建SingletonClass.h文件

#import <Foundation/Foundation.h>
#import "AVFoundation/AVFoundation.h"

@interface SingletonClass : NSObject 
{
    BOOL is_sound_enable;
    AVAudioPlayer *audioPlayer;
}

@property (nonatomic,assign) BOOL is_sound_enable;
@property (nonatomic,retain) AVAudioPlayer *audioPlayer;

+ (SingletonClass *)sharedInstance;
-(void)checkAndPlayMusic;
-(void)loadNewFile:(NSURL*)newFileURL;

@end

按如下方式创建SingletonClass.m文件

#import "SingletonClass.h"

@implementation SingletonClass

@synthesize is_sound_enable;
@synthesize audioPlayer;

#pragma mark -
#pragma mark Singleton Variables
static SingletonClass *singletonHelper = nil;

#pragma mark -
#pragma mark Singleton Methods
- (id)init {
    if ((self = [super init])) {
        is_sound_enable = YES;
        NSString *strPath = @""; //<-- Assign path here
        NSURL *url = [NSURL URLWithString:strPath];
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [audioPlayer prepareToPlay];
        audioPlayer.numberOfLoops = -1; //<-- This will set it to infinite playing.
    }

    return self;
}
+ (SingletonClass *)sharedInstance {
    @synchronized(self) {
        if (singletonHelper == nil) {
            [[self alloc] init]; // assignment not done here
        }
    }
    return singletonHelper;
}
+ (id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (singletonHelper == nil) {
            singletonHelper = [super allocWithZone:zone];
            // assignment and return on first allocation
            return singletonHelper;
        }
    }
    // on subsequent allocation attempts return nil
    return nil;
}
- (id)copyWithZone:(NSZone *)zone {
    return self;
}
- (id)retain {
    return self;
}
- (unsigned)retainCount {
    return UINT_MAX;  // denotes an object that cannot be released
}
//- (void)release {
- (void)dealloc {
    [audioPlayer release];
    [super dealloc];
}
- (id)autorelease {
    return self;
}

-(void)resumeBackgroundMusic
{
    //Your code
    NSLog(@"Playing music");
    [self.audioPlayer play];
}

-(void)pauseBackgroundMusic
{
    //Your code here
    NSLog(@"Paused music");
    [self.audioPlayer pause];
}

-(void)checkAndPlayMusic
{
    if(self.is_sound_enable)
        [self resumeBackgroundMusic];
    else 
        [self pauseBackgroundMusic];
}

//Added this new method to load new music file.
-(void)loadNewFile:(NSURL*)newFileURL
{
    if(self.audioPlayer)
        [self.audioPlayer release];

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newFileURL error:nil];
    [audioPlayer prepareToPlay];
    audioPlayer.numberOfLoops = -1; 
}


@end

现在你需要做的是

SingletonClass *sgHelper = [SingletonClass sharedInstance];
sgHelper.is_sound_enable = NO; //<--Set here NO or YES according to your requirement and music will be played accordingly.
[sgHelper checkAndPlayMusic];

如果你需要进一步的帮助,请告诉我。