iOS开发拓展篇—音乐的播放

一、简单说明

  音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件。

注意:

  (1)该类(AVAudioPlayer)只能用于播放本地音频。

  (2)时间比较短的(称之为音效)使用AudioServicesCreateSystemSoundID来创建,而本地时间较长(称之为音乐)使用AVAudioPlayer类。

 

二、代码示例

  AVAudioPlayer类依赖于AVFoundation框架,因此使用该类必须先导入AVFoundation框架,并包含其头文件(包含主头文件即可)。

说明:iOS8之后,已经不需要导入AVFoundation框架

导入必要的,需要播放的音频文件到项目中

 

代码示例:

 1 //
 2 //  ViewController.m
 3 //  02-音乐播放
 4 //
 5 //  Created by apple on 14/11/7.
 6 //  Copyright (c) 2014年 github. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import <AVFoundation/AVFoundation.h>
11 
12 @interface ViewController ()
13 
14 @end
15 
16 @implementation ViewController
17 
18 - (void)viewDidLoad
19 {
20     [super viewDidLoad];
21     
22 }
23 
24 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
25 {
26     
27     // 1.音频文件的url路径
28     NSURL *url = [[NSBundle mainBundle]URLForResource:@"简单爱.mp3" withExtension:Nil];
29     
30     // 2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
31     AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:Nil];
32     
33     // 3.缓冲
34     [player prepareToPlay];
35     
36     // 4.播放
37     [player play];
38 }
39 
40 @end

代码说明:运行程序,点击模拟器界面,却并没有能够播放音频文件,原因是代码中创建的AVAudioPlayer播放器是一个局部变量,应该调整为全局属性。

可将代码调整如下,即可播放音频:

 1 #import "ViewController.h"
 2 #import <AVFoundation/AVFoundation.h>
 3 
 4 @interface ViewController ()
 5 // 播放器
 6 @property(nonatomic,strong)AVAudioPlayer *player;
 7 @end
 8 
 9 @implementation YYViewController
10 
11 - (void)viewDidLoad
12 {
13     [super viewDidLoad];
14     
15 }
16 
17 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
18 {
19     
20     // 1.音频文件的url路径
21     NSURL *url = [[NSBundle mainBundle] URLForResource:@"简单爱.mp3" withExtension:ni];
22     
23     // 2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
24     self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:Nil];
25     
26     // 3.缓冲
27     [self.player prepareToPlay];
28     
29     // 4.播放
30     [self.player play];
31 }
32 
33 @end

注意:一个AVAudioPlayer只能播放一个url,如果想要播放多个文件,那么就得创建多个播放器。

 

三、相关说明

新建一个项目,在storyboard中放四个按钮,分别用来控制音乐的播放、暂停、停止和下一首。

  

 

程序代码如下:

注意:如果点了“停止”,那么一定要播放器重新创建,不然的话会出现莫名其妙的问题。

  点击了stop之后,播放器实际上就不能再继续使用了,如果还继续使用,那么后续的一些东西会无法控制。

推荐代码:

 1 #import "ViewController.h"
 2 #import <AVFoundation/AVFoundation.h>
 3 #import "HMAudioTool.h"
 4 
 5 @interface ViewController ()
 6 //  播放器
 7 @property (nonatomic, strong) AVAudioPlayer *player;
 8 
 9 // 播放
10 - (IBAction)playMusic;
11 
12 // 暂停
13 - (IBAction)pauseMusic;
14 
15 // 停止
16 - (IBAction)stopMusic;
17 
18 // 下一首
19 - (IBAction)nextMusic;
20 @end
21 
22 @implementation YYViewController
23 
24 #pragma mark-懒加载
25 - (AVAudioPlayer *)player
26 {
27     if (!_player)
28     {
29         // 0.加载音乐文件
30         NSURL *url = [[NSBundle mainBundle] URLForResource:@"简单爱.mp3" withExtension:nil];
31         // 1.创建音乐播放器
32         self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
33         // 2.缓冲音乐文件(准备)
34         [self.player prepareToPlay];
35     }
36     return _player;
37 }
38 
39 - (void)viewDidLoad
40 {
41     [super viewDidLoad];
42 }
43 
44 - (IBAction)playMusic {
45     // 播放器的url时readonly的, 所以不能设置, 也就是说一个播放器对应一首歌
46     //    self.player.url = nil;
47     // 开始播放/继续播放
48     [self.player play];
49 }
50 
51 - (IBAction)pauseMusic {
52     // 暂停
53     [self.player pause];
54 }
55 
56 - (IBAction)stopMusic {
57     // 停止
58     [self.player stop];
59 #warning 注意: 如果播放器销毁,播放器就不可以用了,因为如果播放器销毁后就不可控制器了,建议每次调用播放器的销毁方法之后都清空播放器
60     self.player = nil;
61 
62 }
63 
64 - (IBAction)nextMusic {
65 
66 }
67 
68 @end

如果点击了停止按钮,那么音乐会从头开始播放。

 

四、播放多个文件

  self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

  self.player.url;

  点击,url,按住common建查看。

可以发现,这个url是只读的,因此只能通过initWithContentsOfUrl的方式进行设置,也就意味着一个播放器对象只能播放一个音频文件。

那么如何实现播放多个音频文件呢?

可以考虑封装一个播放音乐的工具类,下一篇文章将会介绍具体怎么实现。

posted @ 2016-08-13 17:14  一叶知秋的码拉松  阅读(267)  评论(0编辑  收藏  举报