xcode添加音效
背景音乐:http://www.cnblogs.com/jiayongqiang/p/5476069.html
音效:
一.介绍:
又称“短音频”,通常在程序中的播放时长为1~2秒.最长可以播放29.9s.
在最后会讲解如何将长音乐MP3格式,制作成xcode能识别的音效格式(wav/aac/m4a等等).
二、音效的播放
1.获得音效文件的路径
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
2.加载音效文件,得到对应的音效ID
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3.播放音效
AudioServicesPlaySystemSound(soundID);
注意:音效文件只需要加载1次
4.音效播放常见函数总结
加载音效文件 AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
释放音效资源 AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
播放音效 AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放音效带点震动 AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
#import "ViewController.h" //1.导入框架
#import <AVFoundation/AVFoundation.h> @interface ViewController () //2.定义一个SystemSoundID属性
@property (nonatomic, assign) SystemSoundID soundID;
@end @implementation ViewController //3.懒加载SoundID
- (SystemSoundID)soundID {
if (_soundID == ) {
// 3.1.获取资源的URL,音频文件要放在黄色文件夹中
NSURL *buyaoUrl = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil]; // 3.2给SoundID赋值
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(buyaoUrl), &_soundID);
}
return _soundID;
} - (void)viewDidLoad {
[super viewDidLoad];
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 4.1.播放音效,没有振动效果
AudioServicesPlaySystemSound(self.soundID);
// 4.2.播放音效,在真机上带有振动效果
// AudioServicesPlayAlertSound(soundID);
}
三.将播放音效的功能封装成一个工具类.
1.创建一个类PQAudioTool继承自NSObject.
PQAudioTool.h文件:
#import <Foundation/Foundation.h> @interface PQAudioTool : NSObject
/**
* 播放音效
* @param soundName 音效名称
*/
+ (void)audioToolPlaySoundWithName:(NSString *)soundName;
/**
* 销毁音效
* @param soundName 音效名称
*/
+ (void)audioToolDisposeSoundWihtName:(NSString *)soundName;
@end
PQAudioTool.m文件:
#import "PQAudioTool.h"
#import <AVFoundation/AVFoundation.h> @interface PQAudioTool()
//在此处定义一个成员属性. 那么在类方法中无法取到.所以需要定义一个静态成员属性.
@end @implementation PQAudioTool //定义一个静态成员属性,会一直保存在内存中.属于类的. 不属于哪一个对象的.
//定义一个字典放赋值的soundID.
static NSMutableDictionary *_soundIDs;
+ (void)initialize {//该方法只会调用一次(前提是该类没有子类).
_soundIDs = [NSMutableDictionary dictionary];
}
+ (void)audioToolPlaySoundWithName:(NSString *)soundName {
// 1.从字典中取出音效的ID
SystemSoundID soundID = [_soundIDs[soundName] unsignedIntValue];
// 2.如果现在取出来的值是0
if (soundID == ) {
// 2.1.获取资源的URL
NSURL *buyaoUrl = [[NSBundle mainBundle] URLForResource:soundName withExtension:nil];
// 2.2.给SoundID赋值
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(buyaoUrl), &soundID);
// 2.3.放入字典中
_soundIDs[soundName] = @(soundID);
}
// 3.播放音效
AudioServicesPlaySystemSound(soundID);
}
+ (void)audioToolDisposeSoundWihtName:(NSString *)soundName {
// 1.从字典中取出音效的ID
SystemSoundID soundID = [_soundIDs[soundName] unsignedIntValue];
// 2.如果soundID有值,则销毁音效
if (soundID) {
AudioServicesDisposeSystemSoundID(soundID);
}
}
@end
在viewController里调用
- (IBAction)buyaoClick:(id)sender {
[PQAudioTool audioToolPlaySoundWithName:@"1-02 何かがおかしい.m4a"];
}
- (IBAction)oneClick:(id)sender {
[PQAudioTool audioToolPlaySoundWithName:@"lose.aac"];
}
- (IBAction)twoClick:(id)sender {
[PQAudioTool audioToolPlaySoundWithName:@"m_04.wav"];
}
四:将长音乐制作成小于30s的音效 1.打开iTunes

2.将需要制作的音乐导入iTunes
3.在iTunes中选择音乐-->右击-->显示简介

4.选择选项卡-->设置开始/结束时间-->好

5.选中音乐-->文件-->转换-->AAC

6.成功.直接将转换好的短音乐 拖拽出来即可使用.

xcode添加音效的更多相关文章
- xcode添加背景音乐/音效
xcode添加音效:http://www.cnblogs.com/jiayongqiang/p/5625886.html 背景音乐: ios播放音乐时会用到一个叫做AVAudioPlayer的类,这个 ...
- [Unity3D]添加音效说明
添加音效组件并添加音乐资源 其中Pitch用来提高和降低音调的,比如可以和赛车游戏的轮胎绑定,当轮胎越快,则音调越高 2D/3D音效:2D音效和摄影家的距离无关,可以看做是一个背景音乐:而3D音效则是 ...
- Xcode添加摄像机访问权限<转>
转帖地址:http://www.manew.com/thread-97708-1-1.html ============================================== ios系统 ...
- IOS开发效率之为Xcode添加常用的代码片段
IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...
- xcode添加build phase
[xcode添加build phase] xcode版本:5.0.2,找了半天,终于找到add build phase的方法,如下图.
- xcode添加快捷代码块
添加快捷代码块 在开发过程中,一些常用的代码段,我们可以设置成快捷代码段来快速实现代码的编写. 如上图中的属性的设置,它们都有相同重复的代码@property (nonatomic, strong), ...
- 【转】为Xcode添加删除行、复制行快捷键
原文网址:http://www.jianshu.com/p/cc6e13365b7e 在使用eclipse过程中,特喜欢删除一行和复制一行的的快捷键.而恰巧Xcode不支持这两个快捷键,再一次的恰巧让 ...
- (转)为Xcode添加删除行、复制行快捷键
转摘链接:http://www.jianshu.com/p/cc6e13365b7e 在使用eclipse过程中,特喜欢删除一行和复制一行的的快捷键.而恰巧Xcode不支持这两个快捷键,再一次的恰巧让 ...
- 62.Xcode 添加代码块
1. Xcode创建一个新项目,打开一个.h或者.m文件 2.我举例以设置属性为例 #import <UIKit/UIKit.h> @interface ViewController : ...
随机推荐
- Linux6(5)安装Oracle Rac11g
1.创建用户组.以root身份运行以下命令:/usr/sbin/groupadd -g 501 oinstall /usr/sbin/groupadd -g 502 dba /usr/sbin/gro ...
- Mysql备份迁移——MySqlBackup(.net)——(无法解决视图嵌视图报错)
这里是利用MySqlBackup,可以再nuget中下载. 无法解决视图嵌视图报错的问题,只导表跟数据比较合适,如果有视图嵌视图,请参照Mysql备份迁移——Mysqldump(.NET调用Mysql ...
- java中常用数据类型转换器
/** * 把String转换成long * * @param src 要转换的String * @param def 转换失败时返回此值 * @return 转换好的long */ public s ...
- centos7.2上实践cgoup
基本介绍 CGroups 是一种对进程资源管理和控制的统一框架,它提供的是一种机制,而具体的策略(Policy)是通过子系统(subsystem)来完成的.子系统是CGroups对进程组进行资源控制的 ...
- 使用percona-xtrabackup实现对线上zabbix监控系统数据库mariadb5.5.47的主从同步
使用percona-xtrabackup实现对线上zabbix监控系统数据库的主从同步 业务背景: zabbix3.0.4是业务的主要监控,部署在一台单机中,为避免数据丢失先对其做数据主从同步,因主数 ...
- 用tcc遇到的一个大坑
在centos6.5 x86_64服务器上编译安装完tcc, 版本0.9.25(在github上clone的),似乎一切正常 但当用tcc来编译"hello, world"程序时, ...
- 通过微信企业号发送zabbix报警
采用微信报警时,管理员账户中必须要设置"示警媒体"一项,"收件人"一项可随便填写一下.其它成员则可以不用添加设置. ---------------------- ...
- 在eclipse中运行perl代码,需要配置的插件以及方法
Eclipse配置正则表达式 网址:http://www.cnblogs.com/itech/archive/2010/02/23/1671676.html perl的环境配置以及在Eclipse中安 ...
- [2015hdu多校联赛补题]hdu5324 Boring Class
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324 题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小) ...
- Spring MVC开发环境的搭建和实例
一.安装jdk 二.安装tomcat 三.安装maven 新增环境变量MAVEN-HOME,并在path变量中添加bin路径 四.安装IntelliJ IDEA 五.创建maven web项目选择jd ...