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

     xcode添加音效-LMLPHP

  2.将需要制作的音乐导入iTunes

    xcode添加音效-LMLPHP      

  3.在iTunes中选择音乐-->右击-->显示简介

    xcode添加音效-LMLPHP

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

    xcode添加音效-LMLPHP

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

      xcode添加音效-LMLPHP

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

      xcode添加音效-LMLPHP

05-11 18:17