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


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

    一、简单说明

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

    注意:

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

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

    二、代码示例

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

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

    代码示例:

    //
    //  YYViewController.m
    //  15-播放音乐
    //
    
    #import "YYViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    @interface YYViewController ()
    
    @end
    
    @implementation YYViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        
        //1.音频文件的url路径
        NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
        
        //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
        AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
        
        //3.缓冲
        [audioPlayer prepareToPlay];
        
        //4.播放
        [audioPlayer play];
    }
    
    @end

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

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

    #import "YYViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    @interface YYViewController ()
    @property(nonatomic,strong)AVAudioPlayer *audioplayer;
    @end
    
    @implementation YYViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        
        //1.音频文件的url路径
        NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
        
        //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
        self.audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
        
        //3.缓冲
        [self.audioplayer prepareToPlay];
        
        //4.播放
        [self.audioplayer play];
    }
    
    @end

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

    三、相关说明

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

    程序代码如下:

    #import "YYViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    @interface YYViewController ()
    @property(nonatomic,strong)AVAudioPlayer *player;
    - (IBAction)play;
    - (IBAction)pause;
    - (IBAction)stop;
    @end
    
    @implementation YYViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        //1.音频文件的url路径
        NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
        
        //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
        self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
        
        //3.缓冲
        [self.player prepareToPlay];
    
    }
    
    - (IBAction)play {
        //开始播放/继续播放
        [self.player play];
    }
    
    - (IBAction)pause {
        //暂停
        [self.player pause];
    }
    
    - (IBAction)stop {
        //停止
        //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题
        [self.player stop];
    }
    @end

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

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

    推荐代码:

    #import "YYViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    @interface YYViewController ()
    @property(nonatomic,strong)AVAudioPlayer *player;
    - (IBAction)play;
    - (IBAction)pause;
    - (IBAction)stop;
    @end
    
    @implementation YYViewController
    
    #pragma mark-懒加载
    -(AVAudioPlayer *)player
    {
        if (_player==Nil) {
            
            //1.音频文件的url路径
            NSURL *url=[[NSBundle mainBundle]URLForResource:@"235319.mp3" withExtension:Nil];
            
            //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
            self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
            
            //3.缓冲
            [self.player prepareToPlay];
        }
        return _player;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    
    - (IBAction)play {
        //开始播放/继续播放
        [self.player play];
    }
    
    - (IBAction)pause {
        //暂停
        [self.player pause];
    }
    
    - (IBAction)stop {
        //停止
        //注意:如果点击了stop,那么一定要让播放器重新创建,否则会出现一些莫名其面的问题
        [self.player stop];
        self.player=Nil;
    }
    @end

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

    四、播放多个文件

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

     

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

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

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

  • 相关阅读:
    IO复习
    递归
    转换流
    编码与解码
    打印流(printStream)
    Properties
    【转】将Visual Studio武装到底
    【转】VS2008中的自定义格式化代码
    C++开发工具的常用插件
    抽烟的注意事项
  • 原文地址:https://www.cnblogs.com/yipingios/p/5566165.html
Copyright © 2020-2023  润新知