国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS音頻

2019-11-09 17:47:17
字體:
來源:轉載
供稿:網友
 1、關于音效                              音效又稱短音頻,是一個聲音文件,在應用程序中起到點綴效果,用于提升應用程序的整體用戶體驗。我們手機里常見的APP幾乎都少不了音效的點綴。顯示實現音效并不復雜,但對我們App很重要! 2、音效播放                              2.1、首先實現我們需要導入框架AudioToolbox.framework                                

2.2、為了優化效播放,減少每次重復加載音效播放,我們將加載音效設為單例

實現單例 —— 將我在前幾篇文章說過封裝好的的單例宏 直接引用 Singleton.h

創建Singleton.h
#import <Foundation/Foundation.h>#import "Singleton.h"@interface SoundTools : NSObject// 單例宏singleton_interface(SoundTools)// 要播放的音效名- (void)playSoundWithName:(NSString *)name;@end將APP要用到的音效添加到新建的bound里去如圖:創建Singleton.m
#import "SoundTools.h"#import <AudioToolbox/AudioToolbox.h>/** 將所有的音頻文件在此單例中統一處理 */@interface SoundTools(){     NSDictionary  *_soundDict; // 音頻字典}@end@implementation SoundToolssingleton_implementation(SoundTools)- (id)init{     self = [super init];     if (self)    {         // 完成所有音頻文件的加載工作         _soundDict = [self loadSounds];     }     return self;}2.3、啟動系統聲音服務                                                系統聲音服務通過SystemSoundID來播放聲音文件,對于同一個聲音文件,可以創建多個SystemSoundI。系統聲音服務是一套C語言的框架。為了提高應用程序性能,避免聲音文件被重復加載,通常采用單例模式處理系統聲音的播放Singleton.m 實現
#PRagma mark 加載指定的音頻文件- (SystemSoundID)loadSoundWithURL:(NSURL *)url{    SystemSoundID soundID = 0;    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);     return soundID;}

2.4、加載bound里所有的音效文件,并記錄進全局的字典中

#pragma mark 加載所有的音頻文件- (NSDictionary *)loadSounds{     // 思考:如何直到加載哪些音頻文件呢?     // 建立一個sound.bundle,存放所有的音效文件     // 在程序執行時,直接遍歷bundle中的所有文件     // 1. 取出bundle的路徑名    NSString *mainBundlPath = [[NSBundle mainBundle] bundlePath];    NSString *bundlePath = [mainBundlPath stringByAppendingPathComponent:@"sound.bundle"];    // 2. 遍歷目錄    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:nil];    // 3. 遍歷數組,創建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、播放音頻                                                   注意 斷言:在項目開發中,防止被無意中修改音效名,找不到要播放的音效文件
#pragma mark - 播放音頻- (void)playSoundWithName:(NSString *)name{     SystemSoundID soundID = [_soundDict[name] unsignedLongValue];    NSLog(@"%ld",soundID);     //斷言它必須大于0;    NSAssert(soundID > 0, @"%@ 聲音文件不存在!", name);    AudioServicesPlaySystemSound(soundID);}在控制器里調用按鈕的點擊事情即可
- (void)clickMe{     [[SoundTools sharedSoundTools] playSoundWithName:@"game_overs.mp3"];}2.6、優化之前的代碼 —— 每次都會重復加載新的音效
// NSURL *url = [[NSBundle mainBundle] URLForResource:@"bullet.mp3" withExtension:nil];// SystemSoundID soundID = 0;// //  // 創建聲音,并且生成soundID的數值// AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);////  // 播放聲音//  // 同樣遵守蘋果的靜音原則,如果用戶靜音,會震動!提示用戶注意!// // AudioServicesPlayAlertSound(<#SystemSoundID inSystemSoundID#>)//  // 只播放聲音,遵守蘋果的靜音原則 HIG// AudioServicesPlaySystemSound(soundID);//// NSLog(@"%ld", soundID);
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜梁县| 丰原市| 庆安县| 东乌珠穆沁旗| 临海市| 邵阳市| 灵石县| 浦县| 阿克| 崇明县| 石泉县| 顺平县| 嵊泗县| 红安县| 义马市| 景洪市| 大冶市| 宜阳县| 张家港市| 中方县| 大石桥市| 潞城市| 姚安县| 昌乐县| 长武县| 铜山县| 永川市| 重庆市| 临湘市| 江城| 文山县| 内黄县| 博罗县| 本溪| 巴林右旗| 来安县| 萍乡市| 阳曲县| 牟定县| 石河子市| 峡江县|