从Sprite工具包中的类调用方法?

时间:2014-01-15 04:57:36

标签: xcode audio methods sprite-kit

好的,有时间问。我的player.m文件中有一个名为musicOn的方法。这是我的课程。在启动时它播放音乐,并播放所有场景,但我的MainMenu中的按钮关闭和开启音乐不起作用。我做错了什么?

Player.h

#import <Foundation/Foundation.h>

@interface Player : NSObject{


}

+ (Player *) musicOn;
+ (Player *) musicOff;

@end

Player.m

#import "Player.h"
#import <AVFoundation/AVFoundation.h>
@import AVFoundation;

@interface Player()

@property (nonatomic, strong) AVAudioPlayer * backgroundMusicPlayer;

@end

static Player *musicOff = nil;
static Player *musicOn = nil;

@implementation Player

//Music on Method.
+ (Player *) musicOn {

NSLog(@"Music on method called");

if (!musicOn) {

    musicOn = [[super allocWithZone:nil]init];

    //Play Music.
    NSError *error;
    NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"music" withExtension:@"wav"];
    musicOn.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
    musicOn.backgroundMusicPlayer.numberOfLoops = -1;
    [musicOn.backgroundMusicPlayer prepareToPlay];
    [musicOn.backgroundMusicPlayer play];
    NSLog(@"Music on");

}
    return musicOn;

}

//Music off Method.
+ (Player *) musicOff {

NSLog(@"Music off method called");

if (!musicOff) {

    musicOff = [[super allocWithZone:nil]init];

    [musicOff.backgroundMusicPlayer stop];

    NSLog(@"Music off");
}

return musicOff;

}

//Overwriting allocWithZone.
+(id) allocWithZone:(NSZone *)zone {

return [self musicOn];
return [self musicOff];
}

//Overwriting init.
- (id) init {

self = [super init];

if (self) {

    _backgroundMusicPlayer = self.backgroundMusicPlayer;

}
return self;
}


@end

MainMenu.m

//Calling the method from Player class.
[Player musicOn];

日志显示此

2014-01-16 21:23:04.010 Sprite Test[12582:60b] Music on method called
2014-01-16 21:23:04.035 Sprite Test[12582:60b] Music on
2014-01-16 21:23:04.065 Sprite Test[12582:60b] Music on
2014-01-16 21:23:04.068 Sprite Test[12582:60b] soundOn Called

2014-01-16 21:23:08.641 Sprite Test[12582:60b] Music off method called
2014-01-16 21:23:08.643 Sprite Test[12582:60b] Music off
2014-01-16 21:23:08.646 Sprite Test[12582:60b] Music off
2014-01-16 21:23:08.649 Sprite Test[12582:60b] soundOff Called

2014-01-16 21:23:09.304 Sprite Test[12582:60b] Music on method called
2014-01-16 21:23:09.306 Sprite Test[12582:60b] Music on
2014-01-16 21:23:09.307 Sprite Test[12582:60b] soundOn Called

2 个答案:

答案 0 :(得分:3)

在MainMenu.m中,每次按下按钮时都会创建一个新实例,代码为:ViewController *musicInstance = [[ViewController alloc] init];后跟 [musicInstance musicOn];当你再次关闭音乐时,你也会这样做。

确保您的静态Player变量不在方法内,或者您将创建新的AVAuidioPlayer实例而不是保留一个。 以下代码是否可以帮助您进行Player类?

static Player *musicOn = nil; // <-- Shouldn't be in the method.

+ (Player *)musicOn 
{
    NSLog(@"Music on method called");
    if (!musicOn) 
    {
        musicOn = [[super allocWithZone:nil] init];
        NSError *error;
        NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"music" withExtension:@"wav"];
        musicOn.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
        musicOn.backgroundMusicPlayer.numberOfLoops = -1;
        [musicOn.backgroundMusicPlayer prepareToPlay];
    }
    [musicOn.backgroundMusicPlayer play];
    NSLog(@"Music on");
    return musicOn;
}

答案 1 :(得分:1)

viewController.m中,按如下方式编辑方法-musicOn

if (self.backgroundMusicPlayer == nil)
{
    self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
}

此外,您需要创建viewController类的单例。只有这样,您才能访问播放音乐的特定AVAudioPlayer实例。

请按如下方式更改单身人士的班级方法:

+ (Player *) musicOn {

NSLog(@"Music on method called");

    @synchronized (self)
    {
        if (!musicOn) {

            musicOn = [[super allocWithZone:nil]init];
        }

    }
    //Play Music.
    NSError *error;
    NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"music" withExtension:@"wav"];
    musicOn.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
    musicOn.backgroundMusicPlayer.numberOfLoops = -1;
    [musicOn.backgroundMusicPlayer prepareToPlay];
    [musicOn.backgroundMusicPlayer play];
    NSLog(@"Music on");

}
    return musicOn;

}