2015-02-12 227 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]; 
}