gpt4 book ai didi

xcode - 从Sprite套件中的类调用方法?

转载 作者:行者123 更新时间:2023-12-03 00:06:34 26 4
gpt4 key购买 nike

好,该问了。我的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

最佳答案

在您的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;

}

关于xcode - 从Sprite套件中的类调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21129414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com