(1) AudioPlayer.h
(2) AudioPlayer.m
(3) GameLayer.m
// AudioPlayer.h
// GameDemo
//
// Created by Chang-Min Pak on 6/12/10.
// Copyright 2010 thefirstgoodcom. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
typedef enum {
kAudio_Background
} AudioPlayerType;
@interface AudioPlayer : NSObject {
AVAudioPlayer *backgroundAudioPlayer;
}
@property (nonatomic, retain) AVAudioPlayer *backgroundAudioPlayer;
+ (AudioPlayer *)sharedAudioPlayer;
- (AVAudioPlayer*) createAudioPlayer:(NSString*)fileName fileType:(NSString*)fileType volumn:(CGFloat)volumn;
- (void) playAudio:(AudioPlayerType)type;
- (void) stopAudio:(AudioPlayerType)type;
@end
//
// AudioPlayer.m
// GameDemo
//
// Created by Chang-Min Pak on 6/12/10.
// Copyright 2010 thefirstgood.com. All rights reserved.
//
#import "AudioPlayer.h"
@implementation AudioPlayer
@synthesize backgroundAudioPlayer;
// Singleton
static AudioPlayer *_sharedAudioPlayer = nil;
+ (AudioPlayer *) sharedAudioPlayer {
@synchronized([AudioPlayer class]) {
if (!_sharedAudioPlayer)
[[self alloc] init];
return _sharedAudioPlayer;
}
// to avoid compiler warning
return nil;
}
+ (id) alloc{
@synchronized([AudioPlayer class]) {
_sharedAudioPlayer = [super alloc];
return _sharedAudioPlayer;
}
// to avoid compiler warning
return nil;
}
- (AVAudioPlayer*) createAudioPlayer:(NSString*)fileName fileType:(NSString*)fileType volumn:(CGFloat)volumn {
// 넘겨 받은 파일의 full path를 찾습니다.
NSString *audioPath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileType];
// 메인번들(리소스)에 들어있는 음악파일로 AVAudioPlayer 객체를 만들고 반복 횟수, 소리 세기 등을 설정합니다.
AVAudioPlayer *tmpAudionPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPath] error:NULL];
tmpAudionPlayer.numberOfLoops = 0;
tmpAudionPlayer.volume = volumn;
// 소리를 내기 위해 버퍼에 로딩을 합니다.
// play 메소드를 호출하면 자동으로 호출되게 됩니다.
[tmpAudionPlayer prepareToPlay];
[tmpAudionPlayer autorelease];
return tmpAudionPlayer;
}
// 음악 연주를 시작합니다.
- (void) playAudio:(AudioPlayerType)type {
if(type == kAudio_Background) {
if(self.backgroundAudioPlayer == nil) {
self.backgroundAudioPlayer = [self createAudioPlayer:@"audioBG" fileType:@"wav" volumn:0.7];
// 배경음악이므로 계속해서 반복되도록 합니다.
// -1 = 무한반복
self.backgroundAudioPlayer.numberOfLoops = -1;
}
[self.backgroundAudioPlayer play];
}
}
// 음악 연주를 중단합니다.
- (void) stopAudio:(AudioPlayerType)type {
if(type == kAudio_Background && self.backgroundAudioPlayer != nil) {
[self.backgroundAudioPlayer stop];
self.backgroundAudioPlayer.currentTime = 0;
}
}
- (void) dealloc {
if(self.backgroundAudioPlayer != nil) {
[self stopAudio:kAudio_Background];
[backgroundAudioPlayer release];
}
[super dealloc];
}
@end
(2) 아이폰에서는 다음과 같이 다양한 형태의 음악 파일을 지원합니다.
AAC
AIFF
AAC Protected
MP3
MP3 VBR
Audible
Apple Lossless
WAV
//
// GameLayer.m
// GameDemo
//
// Created by cmpak on 5/10/10.
// Copyright 2010 thefirstgood.com. All rights reserved.
//
#import "AudioPlayer.h"
- (void) onEnter {
[super onEnter];
// 배경음악 연주를 시작합니다.
[[AudioPlayer sharedAudioPlayer] playAudio:kAudio_Background];
}