2012-07-04 69 views
-4

我已經使用一個單獨的類,並使用在所有類別中打開應用聲音開/關。問題是當我改變屏幕時,即使它靜音,聲音也會打開。我在每節課的初始化中都會調用sound方法,因此它會打開。但是如果我不從init調用它永遠不會啓動。我很亂。開啓/關閉全球聲音在cocos2d應用

以下是我所使用

//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]; 
} 
+0

沒有看到代碼它的一些難以幫助你。 –

+0

@Jennis:你能告訴我一個辦法嗎?通過使用全局變量可能會。 – maniclorn

+0

其實你說過你已經實現了singleton類,所以如果你發佈代碼,我們可以檢查代碼的錯誤,否則你需要改變整個東西,如果我建議新的方式。 –

回答

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

讓我知道如果u需要進一步的幫助。

+0

但我在每個場景中都有不同的音樂。因此,如果音樂是混亂的,那麼不要爲該場景播放BGMusic,否則繼續編寫代碼播放音樂。當我切換開關來轉換音樂時,它應該在Singleton類中設置啓用變量,並使用該變量檢查任何場景以暫停/播放音樂。 – maniclorn

+0

是的,所以當你在任何場景中使用最後3行代碼時,你必須做的是根據場景的選擇設置YES或NO,並根據選擇播放/暫停音樂。 –

+0

但是音樂文件路徑是在Singleton類中分配的。如果我正在分配文件路徑,例如。場景第一場景scene1.mp3,場景第二場景scene2.mp3,同樣 - 我應該從單身人士的init [這是分配音頻文件路徑]中刪除代碼?還有什麼需要改進的地方呢? – maniclorn