iOS开发拓展篇—音乐的播放
一、简单说明
音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件。
注意:
(1)该类(AVAudioPlayer)只能用于播放本地音频。
(2)时间比较短的(称之为音效)使用AudioServicesCreateSystemSoundID来创建,而本地时间较长(称之为音乐)使用AVAudioPlayer类。
二、代码示例
AVAudioPlayer类依赖于AVFoundation框架,因此使用该类必须先导入AVFoundation框架,并包含其头文件(包含主头文件即可)。
导入必要的,需要播放的音频文件到项目中。
代码示例:
1 // 2 // YYViewController.m 3 // 15-播放音乐 4 // 5 6 #import "YYViewController.h" 7 #import8 9 @interface YYViewController ()10 11 @end12 13 @implementation YYViewController14 15 - (void)viewDidLoad16 {17 [super viewDidLoad];18 19 }20 21 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event22 {23 24 //1.音频文件的url路径25 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];26 27 //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)28 AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];29 30 //3.缓冲31 [audioPlayer prepareToPlay];32 33 //4.播放34 [audioPlayer play];35 }36 37 @end
代码说明:运行程序,点击模拟器界面,却并没有能够播放音频文件,原因是代码中创建的AVAudioPlayer播放器是一个局部变量,应该调整为全局属性。
可将代码调整如下,即可播放音频:
1 #import "YYViewController.h" 2 #import3 4 @interface YYViewController () 5 @property(nonatomic,strong)AVAudioPlayer *audioplayer; 6 @end 7 8 @implementation YYViewController 9 10 - (void)viewDidLoad11 {12 [super viewDidLoad];13 14 }15 16 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event17 {18 19 //1.音频文件的url路径20 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];21 22 //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)23 self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];24 25 //3.缓冲26 [self.audioplayer prepareToPlay];27 28 //4.播放29 [self.audioplayer play];30 }31 32 @end
注意:一个AVAudioPlayer只能播放一个url,如果想要播放多个文件,那么就得创建多个播放器。
三、相关说明
新建一个项目,在storyboard中放三个按钮,分别用来控制音乐的播放、暂停和停止。
程序代码如下:
1 #import "YYViewController.h" 2 #import3 4 @interface YYViewController () 5 @property(nonatomic,strong)AVAudioPlayer *player; 6 - (IBAction)play; 7 - (IBAction)pause; 8 - (IBAction)stop; 9 @end10 11 @implementation YYViewController12 13 - (void)viewDidLoad14 {15 [super viewDidLoad];16 17 //1.音频文件的url路径18 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];19 20 //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)21 self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];22 23 //3.缓冲24 [self.player prepareToPlay];25 26 }27 28 - (IBAction)play {29 //开始播放/继续播放30 [self.player play];31 }32 33 - (IBAction)pause {34 //暂停35 [self.player pause];36 }37 38 - (IBAction)stop {39 //停止40 //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题41 [self.player stop];42 }43 @end
注意:如果点了“停止”,那么一定要播放器重新创建,不然的话会出现莫名其妙的问题。
点击了stop之后,播放器实际上就不能再继续使用了,如果还继续使用,那么后续的一些东西会无法控制。
推荐代码:
1 #import "YYViewController.h" 2 #import3 4 @interface YYViewController () 5 @property(nonatomic,strong)AVAudioPlayer *player; 6 - (IBAction)play; 7 - (IBAction)pause; 8 - (IBAction)stop; 9 @end10 11 @implementation YYViewController12 13 #pragma mark-懒加载14 -(AVAudioPlayer *)player15 {16 if (_player==Nil) {17 18 //1.音频文件的url路径19 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];20 21 //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)22 self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];23 24 //3.缓冲25 [self.player prepareToPlay];26 }27 return _player;28 }29 30 - (void)viewDidLoad31 {32 [super viewDidLoad];33 }34 35 - (IBAction)play {36 //开始播放/继续播放37 [self.player play];38 }39 40 - (IBAction)pause {41 //暂停42 [self.player pause];43 }44 45 - (IBAction)stop {46 //停止47 //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题48 [self.player stop];49 self.player=Nil;50 }51 @end
如果点击了停止按钮,那么音乐会从头开始播放。
四、播放多个文件
点击,url,按住common建查看。
可以发现,这个url是只读的,因此只能通过initWithContentsOfUrl的方式进行设置,也就意味着一个播放器对象只能播放一个音频文件。
那么如何实现播放多个音频文件呢?
可以考虑封装一个播放音乐的工具类,下一篇文章将会介绍具体怎么实现。