单例课程的音频 [英] Audio With Singleton Class
本文介绍了单例课程的音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个Singleton类来充当音频播放器,该类将在启动时启动音频,并且可以从另一个类中关闭.
I have created a Singleton class to act as an audio player that will start the audio upon launch, and can be turned off from another class.
在另一个班上,当我打电话
In the other class, when I call
[[Singleton singleton] audioPlayer stop];
这给了我错误
'Expected :'
我了解OOP,但是我认为我从未尝试访问对象的对象.有什么想法吗?
I understand OOP but I don't think I've ever tried to access an object of an object. Any ideas?
// 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
推荐答案
在Singleton类中,您可能具有 +(MySingleton *)实例之类的方法
In your Singleton class, you may have the method like + (MySingleton *) instance
因此请使用 [[[Singleton实例] audioPlayer stop];
或
Singleton *singleton = [Singleton instance];
[singleton.audioPlayer stop];
带有NSString的MySingleton示例
MySingleton *mySingleton = [MySingleton instance];
mySingleton.myString = @"hi sigletone";
NSLog(@"%@",mySingleton.myString);
示例MySingleton.h
@interface MySingleton : NSObject
@property (nonatomic,retain) NSString *myString;
+ (MySingleton *) instance;
@end
MySingleton.m示例
#import "MySingleton.h"
@implementation MySingleton
- (id) initSingleton
{
if ((self = [super init]))
{
// Initialization code here.
}
return self;
}
+ (MySingleton *) instance
{
// Persistent instance.
static MySingleton *_default = nil;
// Small optimization to avoid wasting time after the
// singleton being initialized.
if (_default != nil)
{
return _default;
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
// Allocates once with Grand Central Dispatch (GCD) routine.
// It's thread safe.
static dispatch_once_t safer;
dispatch_once(&safer, ^(void)
{
_default = [[MySingleton alloc] initSingleton];
});
#else
// Allocates once using the old approach, it's slower.
// It's thread safe.
@synchronized([MySingleton class])
{
// The synchronized instruction will make sure,
// that only one thread will access this point at a time.
if (_default == nil)
{
_default = [[MySingleton alloc] initSingleton];
}
}
#endif
return _default;
}
@end
这篇关于单例课程的音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文