zoukankan      html  css  js  c++  java
  • iOS开发—音乐的播放

    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的方式进行设置,也就意味着一个播放器对象只能播放一个音频文件。

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

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

  • 相关阅读:
    ent 基本使用十二 字段
    ent 基本使用十一 sql.DB 集成
    ent 基本使用十 数据库迁移
    ent 基本使用九 代码生成
    ent 基本使用八 索引
    ent 基本使用七 Config
    ent 基本使用六 Mixin
    ent 基本使用五 schema介绍
    ent 基本使用四 图遍历查询
    ent 基本使用 三 边(关系处理)
  • 原文地址:https://www.cnblogs.com/jyking/p/4968821.html
Copyright © 2011-2022 走看看