我创建了一个 Singleton 类来充当音频播放器,它将在启动时开始播放音频,并且可以从另一个类中关闭。
在另一个班级,当我打电话时
[[Singleton singleton] audioPlayer stop];
它给了我错误
'Expected :'
我了解 OOP,但我认为我从未尝试过访问对象的对象。有任何想法吗?
// Singleton.m
#import "Singleton.h"
@implementation Singleton
#pragma mark Singleton Methods
+ (id)sharedManager {
static Singleton *singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[self alloc] init];
});
return singleton;
}
- (id)init {
if (self = [super init]) {
boy = false;
girl = false;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Mathletics Theme.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.volume = 0.85f;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
}
return self;
}
@end
// Singleton.h
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface Singleton : NSObject {
AVAudioPlayer *audioPlayer;
}
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
+ (id)singleton;
@end