2015-02-12 225 views
1

尋找一些幫助,試圖讓我的按鈕播放聲音。我查了50多篇教程,但都是過時的,並且不適用於新的Xcode。有沒有人有一個很好的學習資源? Objective-C相當新穎。我已經瀏覽了Apple的文檔,瞭解了Audio框架以及我的腦細胞是否自殺。任何幫助或指針在正確的方向將不勝感激。Xcode 6 - 按下按鈕播放聲音

回答

3

viewController.h

#import <AudioToolbox/AudioToolbox.h>

#import <AVFoundation/AVFoundation.h>

@interface viewController : UIViewController 
@property (strong, nonatomic) AVAudioPlayer *player; 
@end 

viewController.m

-(void)yourButtonDidTap { 
    NSString *soundFilePath = [NSString stringWithFormat:@"%@/%@.mp3", 
           [[NSBundle mainBundle] resourcePath], soundName]; 
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL 
                    error:nil]; 
    _player.numberOfLoops = 0; 

    [_player play]; 
}