·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> 讲解iOS开发中对音效和音乐播放的简单实现

讲解iOS开发中对音效和音乐播放的简单实现

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

音效的播放

一、简单介绍

简单来说,音频可以分为2种

(1)音效

又称“短音频”,通常在程序中的播放时长为1~2秒

在应用程序中起到点缀效果,提升整体用户体验

(2)音乐

  比如游戏中的“背景音乐”,一般播放时间较长

框架:播放音频需要用到AVFoundation.framework框架

二、音效的播放

1.获得音效文件的路径
复制代码 代码如下:
  NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];

2.加载音效文件,得到对应的音效ID
复制代码 代码如下:
  SystemSoundID soundID = 0;

  AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);

3.播放音效
复制代码 代码如下:
  AudioServicesPlaySystemSound(soundID);

 

注意:音效文件只需要加载1次

4.音效播放常见函数总结

加载音效文件
复制代码 代码如下:
  AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)

释放音效资源
复制代码 代码如下:
  AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)

播放音效
复制代码 代码如下:
  AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

播放音效带点震动
复制代码 代码如下:
  AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)

 

三、程序示例

先导入需要依赖的框架

2015111190914661.png (363×152)

导入需要播放的音效文件素材

2015111190944885.png (238×366)

说明:AVFoundation.framework框架中的东西转换为CF需要使用桥接。

代码示例:
复制代码 代码如下:
YYViewController.m文件
//
//  YYViewController.m
//  14-音效播放
//
//  Created by apple on 14-8-8.
//  Copyright (c) 2014年 yangyong. All rights reserved.
//

#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface YYViewController ()

@end

复制代码 代码如下:
@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //1.获得音效文件的全路径
   
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"buyao.wav" withExtension:nil];
   
    //2.加载音效文件,创建音效ID(SoundID,一个ID对应一个音效文件)
    SystemSoundID soundID=0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);
   
    //把需要销毁的音效文件的ID传递给它既可销毁
    //AudioServicesDisposeSystemSoundID(soundID);
   
    //3.播放音效文件
    //下面的两个函数都可以用来播放音效文件,第一个函数伴随有震动效果
    AudioServicesPlayAlertSound(soundID);
    //AudioServicesPlaySystemSound(<#SystemSoundID inSystemSoundID#>)
}

@end

说明:点击屏幕可以播放音效文件。

音乐的播放

一、简单说明

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

注意:

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

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

二、代码示例

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

2015111191006800.png (605×143)

2015111191024806.png (707×191)

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

代码示例:
复制代码 代码如下:
//
//  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中放三个按钮,分别用来控制音乐的播放、暂停和停止。

2015111191047433.png (311×491)

程序代码如下:
复制代码 代码如下:
#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

四、播放多个文件
2015111191115987.png (656×72)
点击,url,按住common建查看。

2015111191137068.png (491×67)

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

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

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