音效的播放
一、简单介绍
简单来说,音频可以分为2种
(1)音效
又称“短音频”,通常在程序中的播放时长为1~2秒
在应用程序中起到点缀效果,提升整体用户体验
(2)音乐
比如游戏中的“背景音乐”,一般播放时间较长
框架:播放音频需要用到AVFoundation.framework框架
二、音效的播放
1.获得音效文件的路径
2.加载音效文件,得到对应的音效ID
  AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3.播放音效
注意:音效文件只需要加载1次
4.音效播放常见函数总结
加载音效文件
三、程序示例
先导入需要依赖的框架

导入需要播放的音效文件素材

说明:AVFoundation.framework框架中的东西转换为CF需要使用桥接。
代码示例:
#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface YYViewController ()
@end
- (void)viewDidLoad
{
    [super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.获得音效文件的全路径
    
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"buyao.wav" withExtension:nil];
    
    //2.加载音效文件,创建音效ID(SoundID,一个ID对应一个音效文件)
    SystemSoundID soundID=0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);
    
    //把需要销毁的音效文件的ID传递给它既可销毁
    //AudioServicesDisposeSystemSoundID(soundID);
    
    //3.播放音效文件
    //下面的两个函数都可以用来播放音效文件,第一个函数伴随有震动效果
    AudioServicesPlayAlertSound(soundID);
    //AudioServicesPlaySystemSound(<#SystemSoundID inSystemSoundID#>)
}
@end
音乐的播放
一、简单说明
音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件。
注意:
(1)该类(AVAudioPlayer)只能用于播放本地音频。
(2)时间比较短的(称之为音效)使用AudioServicesCreateSystemSoundID来创建,而本地时间较长(称之为音乐)使用AVAudioPlayer类。
二、代码示例
  AVAudioPlayer类依赖于AVFoundation框架,因此使用该类必须先导入AVFoundation框架,并包含其头文件(包含主头文件即可)。


导入必要的,需要播放的音频文件到项目中。
代码示例:
#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface YYViewController ()
@end
- (void)viewDidLoad
{
    [super viewDidLoad];
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    //1.音频文件的url路径
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
    
    //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
    AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
    
    //3.缓冲
    [audioPlayer prepareToPlay];
    
    //4.播放
    [audioPlayer play];
}
@end
可将代码调整如下,即可播放音频:
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *audioplayer;
@end
- (void)viewDidLoad
{
    [super viewDidLoad];
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    //1.音频文件的url路径
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
    
    //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
    self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
    
    //3.缓冲
    [self.audioplayer prepareToPlay];
    
    //4.播放
    [self.audioplayer play];
}
@end
三、相关说明
新建一个项目,在storyboard中放三个按钮,分别用来控制音乐的播放、暂停和停止。

程序代码如下:
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *player;
- (IBAction)play;
- (IBAction)pause;
- (IBAction)stop;
@end
@implementation YYViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //1.音频文件的url路径
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
    
    //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
    self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
    
    //3.缓冲
    [self.player prepareToPlay];
}
- (IBAction)play {
    //开始播放/继续播放
    [self.player play];
}
- (IBAction)pause {
    //暂停
    [self.player pause];
}
- (IBAction)stop {
    //停止
    //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题
    [self.player stop];
}
@end
点击了stop之后,播放器实际上就不能再继续使用了,如果还继续使用,那么后续的一些东西会无法控制。
推荐代码:
@interface YYViewController ()
@property(nonatomic,strong)AVAudioPlayer *player;
- (IBAction)play;
- (IBAction)pause;
- (IBAction)stop;
@end
#pragma mark-懒加载
-(AVAudioPlayer *)player
{
    if (_player==Nil) {
        
        //1.音频文件的url路径
        NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
        
        //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
        self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
        
        //3.缓冲
        [self.player prepareToPlay];
    }
    return _player;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (IBAction)play {
    //开始播放/继续播放
    [self.player play];
}
- (IBAction)pause {
    //暂停
    [self.player pause];
}
- (IBAction)stop {
    //停止
    //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题
    [self.player stop];
    self.player=Nil;
}
@end


可以发现,这个url是只读的,因此只能通过initWithContentsOfUrl的方式进行设置,也就意味着一个播放器对象只能播放一个音频文件。
那么如何实现播放多个音频文件呢?
可以考虑封装一个播放音乐的工具类,下一篇文章将会介绍具体怎么实现。