objective-c - Xcode 6 - 按下按钮播放声音

标签 objective-c xcode button avfoundation audiotoolbox

寻求一些帮助,尝试让我的按钮播放声音。我已经查阅了 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/

相关文章:

objective-c - 读取字典元素的预期方法在 'id<NSCopying>' 类型的对象上找不到

ios - 返回 NSString 时 Objective-C 内存泄漏

ios - 更改字体大小以完全适合按钮内的文本?

xcode - 目标依赖与链接二进制文件与库

html - Material 设计 Web 1.0 : How to center the (--full-bleed) button text within a card?

javascript - 使用 PDFBox 2.x 在 PDF 上放置一个按钮

ios - IBAction 循环浏览图像

ios - 如何在 UITableViewCell 之间添加间距

c# - 当我找不到逗号时如何将逗号更改为点

javascript - 选择按钮时如何更改按钮的颜色?