Xcode 6-按下按钮播放声音 [英] Xcode 6 - Press button to play sound

查看:79
本文介绍了Xcode 6-按下按钮播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找一些帮助,以尝试使我的按钮播放声音.我查看了50多个教程,但所有教程都已过时,因此无法与新的Xcode一起使用.有人有很好的学习资源吗?对Objective-C来说还算陌生.我浏览了Apple文档中的Audio框架,并且我的脑细胞自杀了.任何对正确方向的帮助或指导,将不胜感激.

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.

推荐答案

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

这篇关于Xcode 6-按下按钮播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆