iOS开发—音乐的播放

一、简单说明

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

注意:

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

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

二、代码示例

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

  

  

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

代码示例:

 1 //
2 // YYViewController.m
3 // 15-播放音乐
4 //
5
6 #import "YYViewController.h"
7 #import <AVFoundation/AVFoundation.h>
8
9 @interface YYViewController ()
10
11 @end
12
13 @implementation YYViewController
14
15 - (void)viewDidLoad
16 {
17 [super viewDidLoad];
18
19 }
20
21 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
22 {
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 #import <AVFoundation/AVFoundation.h>
3
4 @interface YYViewController ()
5 @property(nonatomic,strong)AVAudioPlayer *audioplayer;
6 @end
7
8 @implementation YYViewController
9
10 - (void)viewDidLoad
11 {
12 [super viewDidLoad];
13
14 }
15
16 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
17 {
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 #import <AVFoundation/AVFoundation.h>
3
4 @interface YYViewController ()
5 @property(nonatomic,strong)AVAudioPlayer *player;
6 - (IBAction)play;
7 - (IBAction)pause;
8 - (IBAction)stop;
9 @end
10
11 @implementation YYViewController
12
13 - (void)viewDidLoad
14 {
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 #import <AVFoundation/AVFoundation.h>
3
4 @interface YYViewController ()
5 @property(nonatomic,strong)AVAudioPlayer *player;
6 - (IBAction)play;
7 - (IBAction)pause;
8 - (IBAction)stop;
9 @end
10
11 @implementation YYViewController
12
13 #pragma mark-懒加载
14 -(AVAudioPlayer *)player
15 {
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)viewDidLoad
31 {
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的方式进行设置,也就意味着一个播放器对象只能播放一个音频文件。

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

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

iOS开发—音乐的播放的更多相关文章

  1. iOS开发--音乐文件播放工具类的封装(包含了音效的封装)

    一.头文件 #import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> @interface ...

  2. iOS开发系列--音频播放(音效和音乐)播放本地的

    音频 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为 点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音频,对于这些音 ...

  3. iOS开发系列--扩展--播放音乐库中的音乐

    众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...

  4. iOS开发系列--音频播放、录音、视频播放、拍照、视频录制

    --iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...

  5. iOS开发系列--音频播放、录音、

    音频 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音频,对于这些音频 ...

  6. iOS开发-音乐播放

    现在的各种App大行其道,其实常用也就是围绕着吃喝玩乐基本的需求,视频,音乐在智能手机出现之前更是必不可少的功能,每个手机都会有一个自带的音乐播放器,当然公众也有自己的需求所以也就造就了各种音乐播放软 ...

  7. iOS开发-- 利用AVPlayer播放远程音乐和视频

    一.简单的播放音乐和视频,播放视频的工具栏需要自己写 二.利用老师封装的框架实现视频播放 链接:http://pan.baidu.com/s/1hrEKlus 密码:8e7g

  8. iOS开发-音乐播放(AVAudioPlayer)

    现在的手机的基本上没有人不停音乐的,我们无法想象在一个没有声音的世界里我们会过的怎么样,国内现在的主流的主流网易云音乐,QQ音乐,酷狗,虾米,天天基本上霸占了所有的用户群体,不过并没有妨碍大家对音乐的 ...

  9. iOS开发之音频播放AVAudioPlayer 类的介绍

    主要提供以下了几种播放音频的方法: 1. System Sound Services System Sound Services是最底层也是最简单的声音播放服务,调用 AudioServicesPla ...

随机推荐

  1. python imaplib无痕取信的主要

    typ, data = M.fetch(num, (UID BODY.PEEK[]))  

  2. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

  3. MySQL学习笔记-数据库内存

    数据库内存 InnoDB存储引擎内存由以下几个部分组成:缓冲池(buffer pool).重做日志缓冲池(redo log buffer)以及额外的内存池(additional memory pool ...

  4. wmi uuid

    [转]https://www.cnblogs.com/-sylar/p/8376621.html 1. 开始-运行-输入:wbemtest 回车2. 单击"连接", 输入:root ...

  5. angular2在双向数据绑定时[(ngModel)]无法使用的问题

    angular2在双向数据绑定时[(ngModel)]无法使用,出现的错误是: Can't bind to 'ngModel' since it isn't a known property of ' ...

  6. maven下的jar项目打包的方法

    1.先对pom项目进行install: RunAs->maven install,如果出现如下错误: Failed to execute goal org.apache.maven.plugin ...

  7. ubuntu 配置ftp server(zz)

    ubuntu 配置 ftp server   安装vsftpd sudo apt-get install vsftpd # vsftp(Very Secure FTP)是一种在Unix/Linux中非 ...

  8. 使用Apache CXF和Spring集成创建Web Service(zz)

    使用Apache CXF和Spring集成创建Web Service 您的评价:       还行  收藏该经验       1.创建HelloWorld 接口类 查看源码 打印? 1 package ...

  9. Redis学习笔记:windows上redis的安装运行

    Redis的windows版本地址https://github.com/MicrosoftArchive/redis 下载之后解压之 在当前解压目录下可以看到如下文件 在当前目录下打开命令行窗口,输入 ...

  10. SpringMVC学习笔记:表单提交 参数的接收

    SpringMVC可以接收原生form表单和json格式数据 有一个名为Book的model,其中的属性如下: 字符串类型的name,数字类型的price,数组类型的cover,集合类型的author ...