'AVFoundation'에 해당되는 글 1건

  1. 2009.08.03 iPhone SDK : AVFoundation을 이용한 간단한 소리 재생

처음  책이나 iPhone SDK Reference를 접하면 범하기 쉬운 오류인 것 같다.

iPhone에서 한 문장 정도의 짧은 오디오 재생을 하려면,
처음으로 권장받는 것은 System Sound Service를 이용하는 것이다.
하지만 이는 짧은 간격으로 오디오를 반복적으로 재생하면 소리가 겹치고,
무엇보다 aiff, wav, caf만 재생이 되는 단점이 있다.
지금 프로젝트에서는 작은 오디오 파일을 3000개 가량 탑재하기 때문에, acc와 같은 압축포맷이 지원돼야 했다.

그래서 사용한게 AVFoundation이다.
앞선 방법에 만만치 않게 쉽고, 간단하다.
이 방법을 늦게 찾아낸 것은 전적으로 한글 책에 의존하고 영어로 된 Reference를 괄시했기 때문이다.
결정적인 순간에 영어가 태클은 건다. 영어 능력은 곧 정보 능력인게지...


NSArray *filenameArr = [filename componentsSeparatedByString:@"."];
NSString *path = [[NSBundle mainBundle] pathForResource:[filenameArr objectAtIndex:0] ofType:[filenameArr objectAtIndex:1]];

/*
// 1. System Sound Service를 이용한 재생
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound(soundID);
*/

// 2. AV Foundation Framwork를 이용한 재생
//// player는 property
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
[fileURL release];

self.player = newPlayer;
[newPlayer release];

[player prepareToPlay];
[player setDelegate: self];
[player play];

Posted by 배트
,