首页 > 代码库 > 音频处理
音频处理
1. 背景音乐
1> 添加AVFoundation.framework框架,import该框架头文件
2> 加载背景音乐路径
NSString *path = [[NSBundle mainBundle] pathForResource:@”music.mp3” ofType:nil];
3> 路径转化成 url ,从本地读取文件,需要使用 fileURLWithPath
NSURL *url = [NSURL fileURLWithPath:path];
4> 初始化AVAudioPlayer
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
5> 设置循环播放次数 0 一次, 1两次, 负数 无线循环
[player setNumberOfLoop:-1];
6> 准备播放 (预加载)
[player prepareToPlay];
7> 播放背景音乐
[player play];
8> 播放音量
[player setVolume:0.5f];
2. 音效
1> 添加AudioToolbox.framework 框架,import 到文件
2> 加载路径
NSString *path = [[NSBundle mainBundle] partForResource:@”sound.aiff” ofType:nil];
3> 转成url格式
NSURL *url = [NSURL fileURLWithPath:path];
4> 定义SystemSoundID
SystemSoundID soundID;
5> 创建音频id
AudioServicesCreateSystemSoundID((_bridge CFURLRef)(url), &soundID);
6> 播放音效
AudioServicesPlaySystemSound(soundID);
7> 播放音效的同时产生震动
AudioServicesPlayAlertSound(soundID);
例子
//// MHViewController.m// 01-石头剪刀布//// Created by ZM on 14-7-15.// Copyright (c) 2014年 MH. All rights reserved.//// 0.png == 石头 1.png == 剪刀 2.png == 布#import "MHViewController.h"#import <AVFoundation/AVFoundation.h>#import <AudioToolbox/AudioToolbox.h>// 输赢信息的缩放比例#define kScaleX 1.5#define kScaleY 1.3// 显示输赢信息动画持续的时间#define kMessageDurationTime 0.5// 出拳动画播放持续的时间#define kDurationTime 1// 音效#define kWinSound @"胜利.aiff"#define kFailureSound @"失败.aiff"#define kDrawSound @"和局.aiff"#define kClickSound @"点击按钮.aiff"// 背景音乐#define kBgMusic @"bgMusic.mp3"@interface MHViewController (){ // 玩家和电脑的得分 NSInteger 没有赋值,默认初始值为 0 NSInteger _plScore; NSInteger _cpScore; // 数组存放图片 NSMutableArray *_imageArray; // 背景音乐 AVAudioPlayer *_bgMusic;}@end@implementation MHViewController- (void)viewDidLoad{ [super viewDidLoad]; _imageArray = [NSMutableArray array]; // 1. 添加需要执行动画的图片 for (int i = 0; i < 3; i++) { NSString *name = [NSString stringWithFormat:@"%d.png", i]; UIImage *image = [UIImage imageNamed:name]; [_imageArray addObject:image]; } // 2. 设置播放数组 [_playerImage setAnimationImages:_imageArray]; [_computerImage setAnimationImages:_imageArray]; // 3. 播放持续时间 [_playerImage setAnimationDuration:kDurationTime]; [_computerImage setAnimationDuration:kDurationTime]; // 4. 开始播放 [_playerImage startAnimating]; [_computerImage startAnimating]; // 5. 初始化背景音乐 _bgMusic = [self initbgWithMusic:kBgMusic loopTime:-1]; [_bgMusic play]; }#pragma mark 音效处理- (SystemSoundID)loadSoundWithSoundName:(NSString *)soundName{ // 1. 加载路径 NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:nil]; // 2. 转成url NSURL *url = [NSURL fileURLWithPath:path]; // 3. SystemSoundID SystemSoundID soundID; // 4. 创建音频id AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); return soundID;}#pragma mark 初始化背景音乐- (AVAudioPlayer *)initbgWithMusic:(NSString *)musicName loopTime:(NSInteger) loopTime{ // 1 加载背景音乐路径 NSString *path = [[NSBundle mainBundle] pathForResource:musicName ofType:nil]; // 2 转成 url. 从本地读取文件,需要使用 fileURLWithPath NSURL *url = [NSURL fileURLWithPath:path]; // 3 初始化 AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; // 4 设置背景音乐循环的次数 0 循环一次, 1 循环两次 负数 无线循环 [_bgMusic setNumberOfLoops:loopTime]; // 5 播放之前都要执行一个操作 : 准备 [_bgMusic prepareToPlay]; // 6 设置音量大小 [_bgMusic setVolume:0.5f]; return player;}#pragma mark 从新开始游戏- (IBAction)resumePlaye:(UIButton *)sender { // 播放点击音效 AudioServicesPlaySystemSound([self loadSoundWithSoundName:kClickSound]); [UIView animateWithDuration:2 animations:^{ // 显示操作按钮 _actionView.hidden = NO; }]; // 开始播放 [_playerImage startAnimating]; [_computerImage startAnimating]; // 恢复 _resultMeaage _resultMessage.transform = CGAffineTransformIdentity; // 隐藏显示信息的Label _resultMessage.hidden = YES;}#pragma mark 玩家出拳// 0.png == 石头 1.png == 剪刀 2.png == 布- (IBAction)playerAction:(UIButton *)sender { // 停止游戏 [_playerImage stopAnimating]; [_computerImage stopAnimating]; // 玩家出拳 NSString *name = [NSString stringWithFormat:@"%d.png", sender.tag]; _playerImage.image = [UIImage imageNamed:name]; // 电脑随机出拳 int num = arc4random_uniform(_imageArray.count); NSString *compImage = [NSString stringWithFormat:@"%d.png", num]; _computerImage.image = [UIImage imageNamed:compImage]; // 调用判断玩家输赢的方法 [self judgeWinWithPlayerTag:sender.tag computerNum:num]; // 执行动画 [self resultWithAnimation];}#pragma mark 判断输赢- (void)judgeWinWithPlayerTag:(NSInteger)playerNum computerNum:(NSInteger)computerNum{ // 玩家赢 0.png == 石头 1.png == 剪刀 2.png == 布 // 左边为电脑 1剪刀 - 0石头 = 1 , 2布 - 1剪刀 = 1 , 玩家 :2布 - 电脑 :0石头 BOOL flage = (computerNum - playerNum == 1) || (playerNum - computerNum == 2); if (flage) { // 玩家赢 // 播放音效 AudioServicesPlaySystemSound([self loadSoundWithSoundName:kWinSound]); _resultMessage.text = @"屌丝, 你赢了"; // 玩家得分 _playerScore.text = [NSString stringWithFormat:@"%d", ++_plScore]; } else if (computerNum == playerNum) { // 平局 AudioServicesPlaySystemSound([self loadSoundWithSoundName:kDrawSound]); _resultMessage.text = @"土豪,我们再玩一局吧"; } else { // 电脑赢 AudioServicesPlaySystemSound([self loadSoundWithSoundName:kFailureSound]); _resultMessage.text = @"屌丝, 你输了"; _computerScore.text = [NSString stringWithFormat:@"%d", ++_cpScore]; }}#pragma mark 显示 出拳解决 执行动画- (void)resultWithAnimation{ // 设置 _resultMeaage 属性 // 添加动画 [UIView animateWithDuration:kMessageDurationTime animations:^{ // 文字缩放 _resultMessage.transform = CGAffineTransformScale(_resultMessage.transform, kScaleX, kScaleY); // 显示信息 _resultMessage.hidden = NO; } completion:^(BOOL finished) { // 动画反转 [UIView animateWithDuration:kMessageDurationTime animations:^{ _resultMessage.transform = CGAffineTransformInvert(_resultMessage.transform); }]; // 显示操作按钮 _actionView.hidden = YES; }];}@end