源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-09-01 16:09 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:讲解iOS开发中对音效和音乐播放的简单实现
[b]音效的播放[/b] 一、简单介绍 简单来说,音频可以分为2种 (1)音效 又称“短音频”,通常在程序中的播放时长为1~2秒 在应用程序中起到点缀效果,提升整体用户体验 (2)音乐   比如游戏中的“背景音乐”,一般播放时间较长 框架:播放音频需要用到AVFoundation.framework框架 二、音效的播放 1.获得音效文件的路径
[u]复制代码[/u] 代码如下:
  NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
2.加载音效文件,得到对应的音效ID
[u]复制代码[/u] 代码如下:
  SystemSoundID soundID = 0;   AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3.播放音效
[u]复制代码[/u] 代码如下:
  AudioServicesPlaySystemSound(soundID);
  注意:音效文件只需要加载1次 4.音效播放常见函数总结 加载音效文件
[u]复制代码[/u] 代码如下:
  AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
释放音效资源
[u]复制代码[/u] 代码如下:
  AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
播放音效
[u]复制代码[/u] 代码如下:
  AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放音效带点震动
[u]复制代码[/u] 代码如下:
  AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
  三、程序示例 先导入需要依赖的框架 [img]http://files.jb51.net/file_images/article/201511/2015111190914661.png?201510119935[/img] 导入需要播放的音效文件素材 [img]http://files.jb51.net/file_images/article/201511/2015111190944885.png?201510119951[/img] 说明:AVFoundation.framework框架中的东西转换为CF需要使用桥接。 代码示例:
[u]复制代码[/u] 代码如下:
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
[u]复制代码[/u] 代码如下:
@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
说明:点击屏幕可以播放音效文件。 [b]音乐的播放[/b] 一、简单说明   音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件。 注意:   (1)该类(AVAudioPlayer)只能用于播放本地音频。   (2)时间比较短的(称之为音效)使用AudioServicesCreateSystemSoundID来创建,而本地时间较长(称之为音乐)使用AVAudioPlayer类。 二、代码示例   AVAudioPlayer类依赖于AVFoundation框架,因此使用该类必须先导入AVFoundation框架,并包含其头文件(包含主头文件即可)。 [img]http://files.jb51.net/file_images/article/201511/2015111191006800.png?2015101191015[/img] [img]http://files.jb51.net/file_images/article/201511/2015111191024806.png?2015101191031[/img] 导入必要的,需要播放的音频文件到项目中。 代码示例:
[u]复制代码[/u] 代码如下:
// //  YYViewController.m //  15-播放音乐 // #import "YYViewController.h" #import <AVFoundation/AVFoundation.h> @interface YYViewController () @end
[u]复制代码[/u] 代码如下:
@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播放器是一个局部变量,应该调整为全局属性。 可将代码调整如下,即可播放音频:
[u]复制代码[/u] 代码如下:
#import "YYViewController.h" #import <AVFoundation/AVFoundation.h> @interface YYViewController () @property(nonatomic,strong)AVAudioPlayer *audioplayer; @end
[u]复制代码[/u] 代码如下:
@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中放三个按钮,分别用来控制音乐的播放、暂停和停止。 [img]http://files.jb51.net/file_images/article/201511/2015111191047433.png?2015101191056[/img] 程序代码如下:
[u]复制代码[/u] 代码如下:
#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之后,播放器实际上就不能再继续使用了,如果还继续使用,那么后续的一些东西会无法控制。 推荐代码:
[u]复制代码[/u] 代码如下:
#import "YYViewController.h" #import <AVFoundation/AVFoundation.h> @interface YYViewController () @property(nonatomic,strong)AVAudioPlayer *player; - (IBAction)play; - (IBAction)pause; - (IBAction)stop; @end
[u]复制代码[/u] 代码如下:
@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
四、播放多个文件 [img]http://files.jb51.net/file_images/article/201511/2015111191115987.png?2015101191121[/img] 点击,url,按住common建查看。 [img]http://files.jb51.net/file_images/article/201511/2015111191137068.png?2015101191144[/img] 可以发现,这个url是只读的,因此只能通过initWithContentsOfUrl的方式进行设置,也就意味着一个播放器对象只能播放一个音频文件。 那么如何实现播放多个音频文件呢? 可以考虑封装一个播放音乐的工具类,下一篇文章将会介绍具体怎么实现。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部