原文地址::https://blog.csdn.net/qq_34047841/article/details/52325481
相关文章
2、iOS开发,音效的播放简单实现以及音效播放的简单封装----https://blog.csdn.net/weixin_33961829/article/details/86393046?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242
3、一文带你实现游戏中的音乐、音效设置----https://www.cnblogs.com/yu97271486/p/11354430.html
4、讲解iOS开发中对音效和音乐播放的简单实现----https://www.jb51.net/article/74666.htm
当参数为:kSystemSoundID_Vibrate 时就是震动;
ps(只有iphone才能震动而且还得在设置里开启震动才行,其他的如touch就没有震动功能,别忘了导入AudioToolbox框架)
当参数为 1000-2000 之间数字时就是播放系统声音
系统声音对照表:
There are some predefined system sounds, for the system sound ID in the range 1000 to 2000 (decimal), as shown below (from 2.0 to 5.0 beta). The system sounds are all stored in /System/Library/Audio/UISounds/.
Sound ID File name (iPhone) File name (iPod Touch) Category Note
1000 new-mail.caf new-mail.caf MailReceived
1001 mail-sent.caf mail-sent.caf MailSent
1002 Voicemail.caf Voicemail.caf VoicemailReceived
1003 ReceivedMessage.caf ReceivedMessage.caf SMSReceived
1004 SentMessage.caf SentMessage.caf SMSSent
1005 alarm.caf sq_alarm.caf CalendarAlert
1006 low_power.caf low_power.caf LowPower
1007 sms-received1.caf sms-received1.caf SMSReceived_Alert
1008 sms-received2.caf sms-received2.caf SMSReceived_Alert
1009 sms-received3.caf sms-received3.caf SMSReceived_Alert
1010 sms-received4.caf sms-received4.caf SMSReceived_Alert
1011 - - SMSReceived_Vibrate
1012 sms-received1.caf sms-received1.caf SMSReceived_Alert
1013 sms-received5.caf sms-received5.caf SMSReceived_Alert
1014 sms-received6.caf sms-received6.caf SMSReceived_Alert
1015 Voicemail.caf Voicemail.caf - Available since 2.1
1016 tweet_sent.caf tweet_sent.caf SMSSent Available since 5.0
1020 Anticipate.caf Anticipate.caf SMSReceived_Alert Available since 4.2
1021 Bloom.caf Bloom.caf SMSReceived_Alert Available since 4.2
1022 Calypso.caf Calypso.caf SMSReceived_Alert Available since 4.2
1023 Choo_Choo.caf Choo_Choo.caf SMSReceived_Alert Available since 4.2
1024 Descent.caf Descent.caf SMSReceived_Alert Available since 4.2
1025 Fanfare.caf Fanfare.caf SMSReceived_Alert Available since 4.2
1026 Ladder.caf Ladder.caf SMSReceived_Alert Available since 4.2
1027 Minuet.caf Minuet.caf SMSReceived_Alert Available since 4.2
1028 News_Flash.caf News_Flash.caf SMSReceived_Alert Available since 4.2
1029 Noir.caf Noir.caf SMSReceived_Alert Available since 4.2
1030 Sherwood_Forest.caf Sherwood_Forest.caf SMSReceived_Alert Available since 4.2
1031 Spell.caf Spell.caf SMSReceived_Alert Available since 4.2
1032 Suspense.caf Suspense.caf SMSReceived_Alert Available since 4.2
1033 Telegraph.caf Telegraph.caf SMSReceived_Alert Available since 4.2
1034 Tiptoes.caf Tiptoes.caf SMSReceived_Alert Available since 4.2
1035 Typewriters.caf Typewriters.caf SMSReceived_Alert Available since 4.2
1036 Update.caf Update.caf SMSReceived_Alert Available since 4.2
1050 ussd.caf ussd.caf USSDAlert
1051 SIMToolkitCallDropped.caf SIMToolkitCallDropped.caf SIMToolkitTone
1052 SIMToolkitGeneralBeep.caf SIMToolkitGeneralBeep.caf SIMToolkitTone
1053 SIMToolkitNegativeACK.caf SIMToolkitNegativeACK.caf SIMToolkitTone
1054 SIMToolkitPositiveACK.caf SIMToolkitPositiveACK.caf SIMToolkitTone
1055 SIMToolkitSMS.caf SIMToolkitSMS.caf SIMToolkitTone
1057 Tink.caf Tink.caf PINKeyPressed
1070 ct-busy.caf ct-busy.caf AudioToneBusy There was no category for this sound before 4.0.
1071 ct-congestion.caf ct-congestion.caf AudioToneCongestion There was no category for this sound before 4.0.
1072 ct-path-ack.caf ct-path-ack.caf AudioTonePathAcknowledge There was no category for this sound before 4.0.
1073 ct-error.caf ct-error.caf AudioToneError There was no category for this sound before 4.0.
1074 ct-call-waiting.caf ct-call-waiting.caf AudioToneCallWaiting There was no category for this sound before 4.0.
1075 ct-keytone2.caf ct-keytone2.caf AudioToneKey2 There was no category for this sound before 4.0.
1100 lock.caf sq_lock.caf ScreenLocked
1101 unlock.caf sq_lock.caf ScreenUnlocked
1102 - - FailedUnlock
1103 Tink.caf sq_tock.caf KeyPressed
1104 Tock.caf sq_tock.caf KeyPressed
1105 Tock.caf sq_tock.caf KeyPressed
1106 beep-beep.caf sq_beep-beep.caf ConnectedToPower
1107 RingerChanged.caf RingerChanged.caf RingerSwitchIndication
1108 photoShutter.caf photoShutter.caf CameraShutter
1109 shake.caf shake.caf ShakeToShuffle Available since 3.0
1110 jbl_begin.caf jbl_begin.caf JBL_Begin Available since 3.0
1111 jbl_confirm.caf jbl_confirm.caf JBL_Confirm Available since 3.0
1112 jbl_cancel.caf jbl_cancel.caf JBL_Cancel Available since 3.0
1113 begin_record.caf begin_record.caf BeginRecording Available since 3.0
1114 end_record.caf end_record.caf EndRecording Available since 3.0
1115 jbl_ambiguous.caf jbl_ambiguous.caf JBL_Ambiguous Available since 3.0
1116 jbl_no_match.caf jbl_no_match.caf JBL_NoMatch Available since 3.0
1117 begin_video_record.caf begin_video_record.caf BeginVideoRecording Available since 3.0
1118 end_video_record.caf end_video_record.caf EndVideoRecording Available since 3.0
1150 vc~invitation-accepted.caf vc~invitation-accepted.caf VCInvitationAccepted Available since 4.0
1151 vc~ringing.caf vc~ringing.caf VCRinging Available since 4.0
1152 vc~ended.caf vc~ended.caf VCEnded Available since 4.0
1153 ct-call-waiting.caf ct-call-waiting.caf VCCallWaiting Available since 4.1
1154 vc~ringing.caf vc~ringing.caf VCCallUpgrade Available since 4.1
1200 dtmf-0.caf dtmf-0.caf TouchTone
1201 dtmf-1.caf dtmf-1.caf TouchTone
1202 dtmf-2.caf dtmf-2.caf TouchTone
1203 dtmf-3.caf dtmf-3.caf TouchTone
1204 dtmf-4.caf dtmf-4.caf TouchTone
1205 dtmf-5.caf dtmf-5.caf TouchTone
1206 dtmf-6.caf dtmf-6.caf TouchTone
1207 dtmf-7.caf dtmf-7.caf TouchTone
1208 dtmf-8.caf dtmf-8.caf TouchTone
1209 dtmf-9.caf dtmf-9.caf TouchTone
1210 dtmf-star.caf dtmf-star.caf TouchTone
1211 dtmf-pound.caf dtmf-pound.caf TouchTone
1254 long_low_short_high.caf long_low_short_high.caf Headset_StartCall
1255 short_double_high.caf short_double_high.caf Headset_Redial
1256 short_low_high.caf short_low_high.caf Headset_AnswerCall
1257 short_double_low.caf short_double_low.caf Headset_EndCall
1258 short_double_low.caf short_double_low.caf Headset_CallWaitingActions
1259 middle_9_short_double_low.caf middle_9_short_double_low.caf Headset_TransitionEnd
1300 Voicemail.caf Voicemail.caf SystemSoundPreview
1301 ReceivedMessage.caf ReceivedMessage.caf SystemSoundPreview
1302 new-mail.caf new-mail.caf SystemSoundPreview
1303 mail-sent.caf mail-sent.caf SystemSoundPreview
1304 alarm.caf sq_alarm.caf SystemSoundPreview
1305 lock.caf sq_lock.caf SystemSoundPreview
1306 Tock.caf sq_tock.caf KeyPressClickPreview The category was SystemSoundPreview before 3.2.
1307 sms-received1.caf sms-received1.caf SMSReceived_Selection
1308 sms-received2.caf sms-received2.caf SMSReceived_Selection
1309 sms-received3.caf sms-received3.caf SMSReceived_Selection
1310 sms-received4.caf sms-received4.caf SMSReceived_Selection
1311 - - SMSReceived_Vibrate
1312 sms-received1.caf sms-received1.caf SMSReceived_Selection
1313 sms-received5.caf sms-received5.caf SMSReceived_Selection
1314 sms-received6.caf sms-received6.caf SMSReceived_Selection
1315 Voicemail.caf Voicemail.caf SystemSoundPreview Available since 2.1
1320 Anticipate.caf Anticipate.caf SMSReceived_Selection Available since 4.2
1321 Bloom.caf Bloom.caf SMSReceived_Selection Available since 4.2
1322 Calypso.caf Calypso.caf SMSReceived_Selection Available since 4.2
1323 Choo_Choo.caf Choo_Choo.caf SMSReceived_Selection Available since 4.2
1324 Descent.caf Descent.caf SMSReceived_Selection Available since 4.2
1325 Fanfare.caf Fanfare.caf SMSReceived_Selection Available since 4.2
1326 Ladder.caf Ladder.caf SMSReceived_Selection Available since 4.2
1327 Minuet.caf Minuet.caf SMSReceived_Selection Available since 4.2
1328 News_Flash.caf News_Flash.caf SMSReceived_Selection Available since 4.2
1329 Noir.caf Noir.caf SMSReceived_Selection Available since 4.2
1330 Sherwood_Forest.caf Sherwood_Forest.caf SMSReceived_Selection Available since 4.2
1331 Spell.caf Spell.caf SMSReceived_Selection Available since 4.2
1332 Suspense.caf Suspense.caf SMSReceived_Selection Available since 4.2
1333 Telegraph.caf Telegraph.caf SMSReceived_Selection Available since 4.2
1334 Tiptoes.caf Tiptoes.caf SMSReceived_Selection Available since 4.2
1335 Typewriters.caf Typewriters.caf SMSReceived_Selection Available since 4.2
1336 Update.caf Update.caf SMSReceived_Selection Available since 4.2
1350 - - RingerVibeChanged
1351 - - SilentVibeChanged
4095 - - Vibrate There was no category for this sound before 2.2.
In the SDK this is the constant kSystemSoundID_Vibrate.
参考: http://iphonedevwiki.net/index.php/AudioServices
playSystemsound demo http://www.cocoachina.com/bbs/job.php?action=download&aid=52041
音效的播放
一、简单介绍
简单来说,音频可以分为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)
三、程序示例
先导入需要依赖的框架
导入需要播放的音效文件素材
说明: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框架,并包含其头文件(包含主头文件即可)。
导入必要的,需要播放的音频文件到项目中。
代码示例:
复制代码代码如下:
//
// 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中放三个按钮,分别用来控制音乐的播放、暂停和停止。
视频会议:上海视盈2016报价【点击进入】
宝利通、思科、LifeSize全部免费试用一个月 12年行业经验,3000多家用户,400-000-5280
查 看
程序代码如下:
复制代码代码如下:
#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的方式进行设置,也就意味着一个播放器对象只能播放一个音频文件。
————————————————
版权声明:本文为CSDN博主「cleven_zhao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_34047841/article/details/52325481