AudioToolbox framework

使用AudioToolbox framework。这个框架可以将比较短的声音注册到 system sound服务上。被注册到system sound服务上的声音称之为 system sounds。它必须满足下面几个条件。

1、 播放的时间不能超过30秒

2、数据必须是 PCM或者IMA4流格式, mp3也可以

3、必须被打包成下面三个格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff),mp3格式也可以

声音文件必须放到设备的本地文件夹下面。通过AudioServicesCreateSystemSoundID方法注册这个声音文件,AudioServicesCreateSystemSoundID需要声音文件的url的CFURLRef对象。看下面注册代码:

- (void)playSoundWithName:(NSString *)name type:(NSString *)type

{

NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:type];

if([[NSFileManager defaultManager] fileExistsAtPath:path]) {

NSURL *url = [NSURL fileURLWithPath:path];

SystemSoundID sound;

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

AudioServicesPlaySystemSound(sound);

}

else {

NSLog(@"**** Sound Error: file not found: %@", path);

}

}

AVFoundation framework

对于压缩过Audio文件,或者超过30秒的音频文件,可以使用AVAudioPlayer类。这个类定义在AVFoundation framework中。

下面我们使用这个类播放一个mp3的音频文件。首先要引入AVFoundation framework,然后MediaPlayerViewController.h中添加下面代码:

#import <</span>AVFoundation/AVFoundation.h>@interface MediaPlayerViewController : UIViewController <</span>AVAudioPlayerDelegate>
{IBOutlet UIButton *audioButton;SystemSoundID shortSound;AVAudioPlayer *audioPlayer;

AVAudioPlayer类也是需要知道音频文件的路径,使用下面代码创建一个AVAudioPlayer实例:

- (id)init
{self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];

if (self) {

NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Music"ofType:@"mp3"];if (musicPath) {NSURL *musicURL = [NSURL fileURLWithPath:musicPath];audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];[audioPlayer setDelegate:self];}NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12"ofType:@"aif"];

我们可以在一个button的点击事件中开始播放这个mp3文件,如:

- (IBAction)playAudioFile:(id)sender
{if ([audioPlayer isPlaying]) {// Stop playing audio and change text of button        [audioPlayer stop];[sender setTitle:@"Play Audio File"forState:UIControlStateNormal];}else {// Start playing audio and change text of button so// user can tap to stop playback        [audioPlayer play];[sender setTitle:@"Stop Audio File"forState:UIControlStateNormal];}
}

这样运行我们的程序,就可以播放音乐了。

这个类对应的AVAudioPlayerDelegate有两个委托方法。一个是 audioPlayerDidFinishPlaying:successfully: 当音频播放完成之后触发。当播放完成之后,可以将播放按钮的文本重新回设置成:Play Audio File

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)playersuccessfully:(BOOL)flag
{[audioButton setTitle:@"Play Audio File"forState:UIControlStateNormal];
}

另一个是audioPlayerEndInterruption:,当程序被应用外部打断之后,重新回到应用程序的时候触发。在这里当回到此应用程序的时候,继续播放音乐。

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{[audioPlayer play];
}

添加按钮声音nbsp;nbsp;播放声音相关推荐

  1. Unity游戏制作问题整理(1)--添加按钮声音

    在制作Unity游戏中,按钮事件是用的比较频繁的,给按钮加声音的方式有两大类. 下面我介绍中,我分别举一个例子.     一种是直接在界面添加(无代码) 首先打开Unity,在场景中新建一个BUTTO ...

  2. 虚拟机共享目录添加按钮灰色_共享按钮将成为PS4最重要的遗产

    虚拟机共享目录添加按钮灰色 I can't say I would have stuck with Ghost of Tsushima - a game I ended up liking a lot ...

  3. jquery用添加按钮把数据传送给PHP页面

    先建一个HTML文件,命名为add.html <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...

  4. iOS 11开发教程(十八)iOS11应用视图之使用代码添加按钮

    iOS 11开发教程(十八)iOS11应用视图之使用代码添加按钮 由于使用编辑界面添加视图的方式比较简单,所以不在介绍.这里,直接讲解代码中如何添加.使用代码为主视图添加一个按钮的方式和在1.3.3节 ...

  5. iOS 9应用开发教程之使用代码添加按钮美化按钮

    iOS 9应用开发教程之使用代码添加按钮美化按钮 丰富的用户界面 在iOS9中提供了很多的控件以及视图来丰富用户界面,对于这些视图以及控件我们在上一章中做了简单的介绍.本章我们将详细讲解这些视图. i ...

  6. pyqt5动态添加按钮

    QMainWindow动态添加按钮,添加后需要调用show(),才可以显示出来. self.start_file_aaa = QPushButton(self) self.start_file_aaa ...

  7. 在使用添加按钮给table插入新的一行时遇见的问题总结及处理方法

    添加按钮的功能:点击添加按钮之后完成添加新的一行. 遇见的问题:当多次点击添加按钮生成新的多行之后,生成的每行内部按钮的保存按钮点击事件出现最晚添加的一行的行内保存点击事件执行一次,倒数第二次添加的行 ...

  8. 在GridView中添加按钮后,如何触发按钮的各种事件?

    //在GridView中添加按钮后,如何触发按钮的各种事件? 1.在GridView的"编辑列"属性窗口中,增加新的Botton列. 2.然后再该窗口右边属性中,将外观项下的But ...

  9. DEV—【GridControl添加按钮列】

    DEV-[GridControl添加按钮列] 效果图 打开GridControl的Run Designer在左侧栏中找到: 添加一个ButtonEdit: 更改属性中的值:Caption为按钮上显示的 ...

最新文章

  1. HTTP请求过程详解
  2. 第三十五课.基于贝叶斯的深度学习
  3. JZOJ 3886. 【长郡NOIP2014模拟10.22】道路维护
  4. HDU 1540 Tunnel Warfare
  5. AutoML大提速,谷歌开源自动化寻找最优ML模型新平台Model Search
  6. 智能写作全景介绍:从最新前沿技术到行业落地解析
  7. Android开发之动态添加控件
  8. 二极管为什么单向导电?
  9. php table 下拉框,LayerUI的table 里面加 select 下拉框 – Fly社区-Mikel
  10. Problem E: 校庆
  11. python注册登录+mysql_Flask+MySql实现用户登录注册
  12. Mysql 解决left join 数据重复的问题
  13. linux网络测试(必会)
  14. Java私人学习笔记——第2章 数据类型和运算符
  15. SHAP (SHapley Additive exPlanations)
  16. token什么意思中文在C语言中,token是什么意思?
  17. Android Activity 生命周期和重要的相关函数(基础一)
  18. 每周分享第 47 期
  19. 网关(Gateway)
  20. matlab 场仿真,利用MATLAB软件实现温度场的仿真

热门文章

  1. amazon - amzreport 之 FBA Inventory Reports
  2. vs2017python如何打包exe_VS2017项目程序打包成.msi或者.exe
  3. 深入理解C语言中两级指针(char **pptr)的参数的用法
  4. Java学习笔记18nbsp;ArrayListnbsp;和Linke…
  5. mysql索引类型normal,unique,full text的区别?
  6. 科技新品 | 索尼首款专业无人机;arpara 5K超清VR头显及一体机;七彩虹多款显卡新品...
  7. 攻击者如何破坏无人机?
  8. 激光功率的测量方案和设备
  9. 洛谷1710 地铁涨价
  10. 最新前端体系学习路径推荐(内附免费资料)