iOS- 優化與封裝 APP音效的播放

 

1.關於音效                    

音效又稱短音頻,是一個聲音文件,在應用程序中起到點綴效果,用於提高應用程序的總體用戶體驗。
 
咱們手機裏常見的APP幾乎都少不了音效的點綴。
 
顯示實現音效並不複雜,但對咱們App很重要!
 

2.音效播放                  

2.1.首先實現咱們須要導入框架AudioToolbox.framework html

 

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裏去優化

如圖:url

建立spa

Singleton.m
#import "SoundTools.h"
#import <AudioToolbox/AudioToolbox.h>

/**
 將全部的音頻文件在此單例中統一處理
 */

@interface SoundTools()
{
    NSDictionary    *_soundDict; // 音頻字典
}

@end

@implementation SoundTools
singleton_implementation(SoundTools)

- (id)init
{
    self = [super init];
    
    if (self) {
        // 完成全部音頻文件的加載工做
        _soundDict = [self loadSounds];
    }
    
    return self;
}

 

2.3.啓動系統聲音服務                   code

系統聲音服務經過SystemSoundID來播放聲音文件,對於同一個聲音文件,能夠建立多個SystemSoundIDhtm

系統聲音服務是一套C語言的框架

爲了提升應用程序性能,避免聲音文件被重複加載,一般採用單例模式處理系統聲音的播放blog

 

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);

 

 

做者: 清澈Saup
出處: http://www.cnblogs.com/qingche/
本文版權歸做者和博客園共有,歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。

相關文章
相關標籤/搜索