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

首頁 > 系統(tǒng) > iOS > 正文

iOS音頻

2019-11-09 16:46:13
字體:
供稿:網(wǎng)友
 1、關(guān)于音效                              音效又稱短音頻,是一個聲音文件,在應(yīng)用程序中起到點(diǎn)綴效果,用于提升應(yīng)用程序的整體用戶體驗(yàn)。我們手機(jī)里常見的APP幾乎都少不了音效的點(diǎn)綴。顯示實(shí)現(xiàn)音效并不復(fù)雜,但對我們App很重要! 2、音效播放                              2.1、首先實(shí)現(xiàn)我們需要導(dǎo)入框架AudioToolbox.framework                                

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);
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石楼县| 阳山县| 明光市| 平武县| 南岸区| 大余县| 崇阳县| 江城| 惠州市| 南宁市| 华阴市| 咸阳市| 海晏县| 绿春县| 叶城县| 游戏| 临沧市| 襄城县| 慈溪市| 辽中县| 班玛县| 土默特右旗| 安岳县| 成安县| 郯城县| 精河县| 保定市| 东海县| 界首市| 敦化市| 宝丰县| 左贡县| 丰镇市| 东乌珠穆沁旗| 新晃| 泾川县| 宜州市| 山东省| 区。| 汽车| 修水县|