-4

シングルトン クラスを使用し、すべてのクラスで使用してアプリのサウンドをオン/オフしました。問題は、ミュートされていても、画面を変更するとサウンドがオンになることです。各クラスの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];
}
4

1 に答える 1

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

さらにサポートが必要な場合はお知らせください。

于 2012-07-04T06:48:33.907 に答える