Xcode 6 - Press button to play sound

佐手、 提交于 2019-12-11 03:51:48

问题


Looking for some help trying to get my button to play sound. I've looked up over 50 tutorials but all of them are outdated and do not work with the new Xcode. Does anyone have a good resource to learn? Fairly new to Objective-C. I've looked through the Apple documentation for the Audio frameworks and my brain cells committed suicide. Any help or pointers in the right direction would be greatly appreciated.


回答1:


in viewController.h

#import <AudioToolbox/AudioToolbox.h>

#import <AVFoundation/AVFoundation.h>

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

and 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];
}


来源:https://stackoverflow.com/questions/28467684/xcode-6-press-button-to-play-sound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!