Iphone 在cocos2d应用程序中全局打开/关闭声音

Iphone 在cocos2d应用程序中全局打开/关闭声音,iphone,ios,cocos2d-iphone,Iphone,Ios,Cocos2d Iphone,我使用了一个单例类,并在所有类中使用来打开/关闭应用程序声音。问题是,当我改变屏幕时,声音会打开,即使它是静音的。我在每个类的init中调用sound方法,所以它会打开。但如果我不从init打电话,它永远不会启动。我搞砸了 下面是我用过的 //SingletonClass.m +(SingletonClass*)sharedMySingleton { ..... } +(id)alloc{ ...... } -(id)init{ self = [super init];

我使用了一个单例类,并在所有类中使用来打开/关闭应用程序声音。问题是,当我改变屏幕时,声音会打开,即使它是静音的。我在每个类的init中调用sound方法,所以它会打开。但如果我不从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];
}

创建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
#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.m文件,如下所示

#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
#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];

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

在没有看到代码的情况下,这是很难帮助您的。@Jennis:您能给我一个建议吗?通过使用全局变量可能是。实际上你说你已经实现了singleton类,所以如果你发布代码,我们可以检查代码是否有错误,或者如果我建议使用新方法,你需要更改整个内容。@Jennis请参考我的代码。现在,当我切换过渡时,音乐会再次打开。@Jennis当前场景中音乐关闭时,场景更改时,音乐会打开,但如果打开,则不会关闭场景更改。但每个场景中的音乐都不同。因此,如果音乐是离散的,那么就不要播放该场景的BGMusic,否则继续编写代码来播放音乐。当我切换开关打开音乐时,它应该在Singleton类中设置enable变量,并使用该变量检查任何场景以暂停/播放音乐。是的,所以当您在任何场景中使用最后3行代码时,您必须根据场景选择设置yes或NO,音乐将根据选择播放/暂停。但是音乐文件路径是在单例类中指定的。如果我正在分配一个文件路径,例如场景一中的scene1.mp3和场景二中的scene2.mp3,同样,我是否应该从singleton的init中删除代码[即分配音频文件路径]?还有什么变化需要做?检查我添加了新的方法加载新的音乐。