2014-06-14 59 views
0

我想每次點擊圖片時都會發出聲音。 添加在 「鏈接框架和庫」 AudioToolbox 和在GameViewController.h 添加:播放音效Xcode 5

#import <AudioToolbox/AudioToolbox.h> 
@interface GameViewController : UIViewController 
{ 
SystemSoundID PlaySoundID; 
} 
- (IBAction)PlayAudioButton:(id)sender; 

(與按鈕連接這一點)。

在GameViewController.m 補充一點:

- (void)viewDidLoad 
{ 

NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Sound" ofType:@"wav"]]; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID); 
} 

- (IBAction)PlayAudioButton:(id)sender { 

AudioServicesPlaySystemSound(PlaySoundID); 

} 

畢竟沒有工作沒有錯誤,但聲音不是沃金... 什麼問題?

謝謝。

回答

0

使用這個代替:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
              pathForResource:@"yourSound" 
              ofType:@"mp3"]]; 

AVAudioPlayer sound = [[AVAudioPlayer alloc] 
        initWithContentsOfURL:url 
        error:nil]; 

[sound play]; 
+0

感謝,但沒有工作.. 「使用未聲明的標識符AVAudioPlayer」 – neKuda

1

不知道如果我正確地理解你的問題,但是這是我怎麼會得到自定義的聲音,當你點擊按鈕

確保

#import <AVFoundation/AVFoundation.h> 
在您的.h和.m文件

創建一個AVAudioPlayer * audioPla

揭掉屬性類(在.H或.M),然後執行以下操作:

-(void)viewDidLoad{ 
    [super viewDidLoad] 
    NSError *error; 
    NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"wav"]; 
    NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; 
    AVAudioPlayer *theAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; 
    [theAudioPlayer prepareToPlay]; 
    self.audioPlayer = theAudioPlayer; 
} 

- (IBAction)PlayAudioButton:(id)sender{ 
    [self.audioPlayer play]; 
} 
+0

由於其工作 – neKuda