저는 개념적으로 이해하지 못하는 것이 있다고 생각합니다. AVAudioPlayer를 확장 한 싱글 톤 AudioPlayer 클래스를 만들었습니다. 내가 한 이유는 내 게임의 배경 음악이기 때문에 나중에 중지하고 시작할 수 있기를 원했기 때문입니다. 여기 내부의 모든 코드는 다음과 같습니다하지만,왜 내 AVAudioPlayer가 재생되지 않습니까?
[[AudioPlayer sharedAudioPlayer] initWithString:[NSString stringWithFormat:@"%@/Music.mp3",[[NSBundle mainBundle] resourcePath]]];
[[AudioPlayer sharedAudioPlayer] play];
아무런 변화가 없습니다 :
#import "AudioPlayer.h"
//THIS IS A SINGLETON CLASS
@implementation AudioPlayer
static AudioPlayer *audioPlayer;
+(AudioPlayer *) sharedAudioPlayer {
return audioPlayer;
}
-(id) initWithString:(NSString *)str {
if (!audioPlayer) {
NSURL *soundFileURL = [NSURL fileURLWithPath:str];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1;
}
return audioPlayer;
}
@end
그래서 나는이 같은 내 게임의 기본보기 컨트롤러에서 클래스를 액세스. 내가 도대체 뭘 잘못하고있는 겁니까?
감사합니다. @ wombat57! 그것은 그것을 설명합니다. 당신은 그것이 싱글 톤일 필요가 없을지도 모른다고 생각합니다. 나는 그것을 다시 생각할 것입니다. –