2015-01-23 4 views

답변

5

사운드를 재생하려면 :

NSString *source = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"mp3"]; 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:source] error:nil]; 
self.audioPlayer.delegate = self; 
self.audioPlayer.numberOfLoops = 0; 
self.audioPlayer.volume = 1.0; 
[self.audioPlayer play]; 

스위프트 2.2 버전 :

var source = NSBundle.mainBundle().pathForResource("beep", ofType: "mp3")! 
do { 
    audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL.fileURLWithPath(source)) 
} 
catch let error { 
    // error handling 
} 
audioPlayer.delegate = self 
audioPlayer.numberOfLoops = 0 
audioPlayer.volume = 1.0 
audioPlayer.play() 

스위프트 3.0 버전 :

var source = Bundle.main.path(forResource: "beep", ofType: "mp3")! 
do { 
    audioPlayer = try AVAudioPlayer(contentsOfURL: URL(fileURLWithPath: source)) 
} 
catch { 
    // error handling 
} 
audioPlayer.delegate = self 
audioPlayer.numberOfLoops = 0 
audioPlayer.volume = 1.0 
audioPlayer.play() 

가 진동하기 :

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 

진동은 Audiotoolbox.framework이고 소리 재생은 AVFoundation.framework입니다.

일부 장치 (iPod Touch)는 진동 할 수 없으며 단순히 작동하지 않습니다.

+0

완벽한 감사! – Kex

+0

@Kex 내 기쁨. :) 어떤 문제가 있으면 알려주세요. 이 답변이 도움이된다면이 답변을 수락하십시오. – KlimczakM

+0

죄송합니다 막 10 분 정도 기다려야했습니다! 덕분에 – Kex