iOS开发—音乐的播放

一、简单说明

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

注意:

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

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

二、代码示例

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

  

  

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

代码示例:

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

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

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

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

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

三、相关说明

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

  

程序代码如下:

  1. 1 #import "YYViewController.h"
  2. 2 #import <AVFoundation/AVFoundation.h>
  3. 3
  4. 4 @interface YYViewController ()
  5. 5 @property(nonatomic,strong)AVAudioPlayer *player;
  6. 6 - (IBAction)play;
  7. 7 - (IBAction)pause;
  8. 8 - (IBAction)stop;
  9. 9 @end
  10. 10
  11. 11 @implementation YYViewController
  12. 12
  13. 13 - (void)viewDidLoad
  14. 14 {
  15. 15 [super viewDidLoad];
  16. 16
  17. 17 //1.音频文件的url路径
  18. 18 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
  19. 19
  20. 20 //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
  21. 21 self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
  22. 22
  23. 23 //3.缓冲
  24. 24 [self.player prepareToPlay];
  25. 25
  26. 26 }
  27. 27
  28. 28 - (IBAction)play {
  29. 29 //开始播放/继续播放
  30. 30 [self.player play];
  31. 31 }
  32. 32
  33. 33 - (IBAction)pause {
  34. 34 //暂停
  35. 35 [self.player pause];
  36. 36 }
  37. 37
  38. 38 - (IBAction)stop {
  39. 39 //停止
  40. 40 //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题
  41. 41 [self.player stop];
  42. 42 }
  43. 43 @end

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

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

推荐代码:

  1. 1 #import "YYViewController.h"
  2. 2 #import <AVFoundation/AVFoundation.h>
  3. 3
  4. 4 @interface YYViewController ()
  5. 5 @property(nonatomic,strong)AVAudioPlayer *player;
  6. 6 - (IBAction)play;
  7. 7 - (IBAction)pause;
  8. 8 - (IBAction)stop;
  9. 9 @end
  10. 10
  11. 11 @implementation YYViewController
  12. 12
  13. 13 #pragma mark-懒加载
  14. 14 -(AVAudioPlayer *)player
  15. 15 {
  16. 16 if (_player==Nil) {
  17. 17
  18. 18 //1.音频文件的url路径
  19. 19 NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
  20. 20
  21. 21 //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
  22. 22 self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
  23. 23
  24. 24 //3.缓冲
  25. 25 [self.player prepareToPlay];
  26. 26 }
  27. 27 return _player;
  28. 28 }
  29. 29
  30. 30 - (void)viewDidLoad
  31. 31 {
  32. 32 [super viewDidLoad];
  33. 33 }
  34. 34
  35. 35 - (IBAction)play {
  36. 36 //开始播放/继续播放
  37. 37 [self.player play];
  38. 38 }
  39. 39
  40. 40 - (IBAction)pause {
  41. 41 //暂停
  42. 42 [self.player pause];
  43. 43 }
  44. 44
  45. 45 - (IBAction)stop {
  46. 46 //停止
  47. 47 //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题
  48. 48 [self.player stop];
  49. 49 self.player=Nil;
  50. 50 }
  51. 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入门科普IDE工具和编译环境

            应友人之邀,今天来讲述python的一些入门内容.本次讲解的并不是语法或者某个模块.                  python下载安装 大多数 Linux 发行版在默认安装的情况 ...

  2. 第一次java实验报告

    实验一Java开发环境的熟悉-1 步骤: mkdir +20165213exp1创建20165213exp1这个目录 cd +20165213zqh进入这个目录 mkdir+src+bin创建目录sr ...

  3. nginx记录post数据日志

    1.vi nginx.conf 找到http {}中log_foramt ,定义post 日志格式 #log_format main '$remote_addr - $remote_user [$ti ...

  4. Gazebo: Could not find parameter robot_description on parameter server

    robot_state_publisher looks for the parameter "robot_description" by default. The robot_st ...

  5. ubuntu配置ftp server

    ubuntu配置ftp server 1. 安装vsftpd   sudo apt-get install vsftpd 安装后会自动新建一个用户ftp,密码ftp,作为匿名用户登录的默认用户 sud ...

  6. 【RabbitMQ】 RabbitMQ安装

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序之间 ...

  7. redis2

    一.安装redis 1)     下载redis安装包 可去官网http://redis.io ,也可通过wget命令, wget http://download.redis.io/redis-sta ...

  8. KBMMW 4.84.00 发布

    kbmMW is a portable, highly scalable, high end application server and enterprise architecture integr ...

  9. Devexpress VCL Build v2015 vol 15.1.2发布

    2015年马上过半年了.终于第一个大版出来了. What's New in 15.1.2 (VCL Product Line)   New Major Features in 15.1 What's ...

  10. 对 Service中sqlsession对象的优化

    在本线程中添加object数据,必须在本线程中才能获取出来..其他线程获取不到. public class Test { public static void main(String[] args) ...