寻求一些帮助,尝试让我的按钮播放声音。我已经查阅了 50 多个教程,但它们都已经过时,并且不适用于新的 Xcode。有没有人有好的学习资源?对 Objective-C 来说相当陌生。我浏览了 Apple 的音频框架文档,我的脑细胞自杀了。任何正确方向的帮助或指示将不胜感激。
最佳答案
在 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];
}
关于objective-c - Xcode 6 - 按下按钮播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28467684/