-1- AuidoSerVices 常用于提示音的播放,該種音頻具有以下四種特點:
1>長度一般不超過30秒,不需要對播放過程進行控制
2>不能循環(huán)播放,不能暫停
3>不能播放立體聲
4>不能播放混音
-2-創(chuàng)建提示音頻AuidoSerVices。。的準備工作
1.添加一個系統(tǒng)類庫。audioToolbox.framework
2.導入頭文件在viewController中 #import <AudioToolbox/AudioToolbox.h>
-3-提示音的幾個常用方法:
1.獲得音效文件的路徑
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
2.加載音效文件,得到對應的音效ID
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3.播放音效
AudioServicesPlaySystemSound(soundID);
4.音效播放的c語言函數(shù)
音效播放的函數(shù)都是基于c語言編寫的,所以在使用的過程中要注意與oc方法使用的區(qū)別
加載音效文件
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
釋放音效資源
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
播放音效
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放音效帶點震動
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
-4-程序簡單實現(xiàn)
#import "RootViewController.h"#import <AudioToolbox/AudioToolbox.h>@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; [self creatUIButton];}-(void)creatUIButton{ UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom]; btn.frame=CGRectMake(100, 100, 100, 100); btn.backgroundColor=[UIColor grayColor]; [btn setTitle:@"播放" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor cyanColor ] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(PRessBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn];}-(void)pressBtn:(id)sender{ //1.獲取全路徑 因為加載音效需要使用CFURLRef 來加載音樂,所以使用能和它相互轉換的NSURL 來轉換路徑 NSString * pathStr= [[NSBundle mainBundle]pathForResource:@"sound" ofType:@"wav"]; NSURL * url=[NSURL fileURLWithPath:pathStr]; //2.創(chuàng)建音效ID 一個ID代表一個音效文件 SystemSoundID SID; //3.將音樂文件與ID綁定 將url 強轉 將sid與之綁定 AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &SID); //4.播放音效 AudioServicesPlayAlertSound(SID); //5.當我們需要在音效播放結束時取消該音效則需要使用以下方法 函數(shù)名固定 AudioServicesAddSystemSoundCompletion(SID, nil, nil,finishSound, nil);}//該函數(shù)為系統(tǒng)方法,函數(shù)名,參數(shù)固定,不能修改void finishSound(SystemSoundID SID,void * finish){ //6.撤銷SID AudioServicesDisposeSystemSoundID(SID);}
注意:點擊播放,就可以播放音效文件
新聞熱點
疑難解答