2.2、為了優(yōu)化效播放,減少每次重復(fù)加載音效播放,我們將加載音效設(shè)為單例
實(shí)現(xiàn)單例 —— 將我在前幾篇文章說過封裝好的的單例宏 直接引用 Singleton.h
創(chuàng)建Singleton.h#import <Foundation/Foundation.h>#import "Singleton.h"@interface SoundTools : NSObject// 單例宏singleton_interface(SoundTools)// 要播放的音效名- (void)playSoundWithName:(NSString *)name;@end將APP要用到的音效添加到新建的bound里去如圖:創(chuàng)建Singleton.m#import "SoundTools.h"#import <AudioToolbox/AudioToolbox.h>/** 將所有的音頻文件在此單例中統(tǒng)一處理 */@interface SoundTools(){ NSDictionary *_soundDict; // 音頻字典}@end@implementation SoundToolssingleton_implementation(SoundTools)- (id)init{ self = [super init]; if (self) { // 完成所有音頻文件的加載工作 _soundDict = [self loadSounds]; } return self;}2.3、啟動系統(tǒng)聲音服務(wù) 系統(tǒng)聲音服務(wù)通過SystemSoundID來播放聲音文件,對于同一個聲音文件,可以創(chuàng)建多個SystemSoundI。系統(tǒng)聲音服務(wù)是一套C語言的框架。為了提高應(yīng)用程序性能,避免聲音文件被重復(fù)加載,通常采用單例模式處理系統(tǒng)聲音的播放Singleton.m 實(shí)現(xiàn)#PRagma mark 加載指定的音頻文件- (SystemSoundID)loadSoundWithURL:(NSURL *)url{ SystemSoundID soundID = 0; AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); return soundID;}2.4、加載bound里所有的音效文件,并記錄進(jìn)全局的字典中
#pragma mark 加載所有的音頻文件- (NSDictionary *)loadSounds{ // 思考:如何直到加載哪些音頻文件呢? // 建立一個sound.bundle,存放所有的音效文件 // 在程序執(zhí)行時(shí),直接遍歷bundle中的所有文件 // 1. 取出bundle的路徑名 NSString *mainBundlPath = [[NSBundle mainBundle] bundlePath]; NSString *bundlePath = [mainBundlPath stringByAppendingPathComponent:@"sound.bundle"]; // 2. 遍歷目錄 NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:nil]; // 3. 遍歷數(shù)組,創(chuàng)建SoundID,如何使用? NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithCapacity:array.count]; [array enumerateObjectsUsingBlock:^(NSString *fileName, NSUInteger idx, BOOL *stop) { // 1> 拼接URL NSString *filePath = [bundlePath stringByAppendingPathComponent:fileName]; NSURL *fileURL = [NSURL fileURLWithPath:filePath]; SystemSoundID soundID = [self loadSoundWithURL:fileURL]; // 將文件名作為鍵值 [dictM setObject:@(soundID) forKey:fileName]; }]; return dictM;}2.5、播放音頻 注意 斷言:在項(xiàng)目開發(fā)中,防止被無意中修改音效名,找不到要播放的音效文件#pragma mark - 播放音頻- (void)playSoundWithName:(NSString *)name{ SystemSoundID soundID = [_soundDict[name] unsignedLongValue]; NSLog(@"%ld",soundID); //斷言它必須大于0; NSAssert(soundID > 0, @"%@ 聲音文件不存在!", name); AudioServicesPlaySystemSound(soundID);}在控制器里調(diào)用按鈕的點(diǎn)擊事情即可- (void)clickMe{ [[SoundTools sharedSoundTools] playSoundWithName:@"game_overs.mp3"];}2.6、優(yōu)化之前的代碼 —— 每次都會重復(fù)加載新的音效// NSURL *url = [[NSBundle mainBundle] URLForResource:@"bullet.mp3" withExtension:nil];// SystemSoundID soundID = 0;// // // 創(chuàng)建聲音,并且生成soundID的數(shù)值// AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);//// // 播放聲音// // 同樣遵守蘋果的靜音原則,如果用戶靜音,會震動!提示用戶注意!// // AudioServicesPlayAlertSound(<#SystemSoundID inSystemSoundID#>)// // 只播放聲音,遵守蘋果的靜音原則 HIG// AudioServicesPlaySystemSound(soundID);//// NSLog(@"%ld", soundID);
新聞熱點(diǎn)
疑難解答
圖片精選