zoukankan      html  css  js  c++  java
  • iOS开发,音效的播放简单实现以及音效播放的简单封装

    一.音效的播放简单实现

    二.音效播放的封装 -- 封装思路:将生成的SystemSoundID存放到字典中,每次播放的时候从字典中取出对应的SystemSoundID,没有的话再创建

    • 头文件中定义类方法                                                                                   
    • 代码实现
      #import "ChaosAudioTool.h"
      #import <AVFoundation/AVFoundation.h>
      // 类方法中不能用成员属性,所以只能定义全局变量
      static NSMutableDictionary *_sounds;
      
      @implementation ChaosAudioTool
      
      // 懒加载字典
      + (void)initialize
      {
          _sounds = [NSMutableDictionary dictionary];
      }
      
      + (void)playAudioWithSoundName:(NSString *)soundName
      {
          if (_sounds[soundName] == nil) { // 先通过字典取,没有的话创建
              SystemSoundID soundID = 0;
              
              NSURL *url = [[NSBundle mainBundle] URLForResource:soundName withExtension:nil];
              
              AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);
              
              // 存入集合
              _sounds[soundName] = @(soundID);
          }
          
          AudioServicesPlaySystemSound([_sounds[soundName] unsignedIntValue]);
      }
  • 相关阅读:
    CSS3实现小黄人动画
    CSS3实现时间轴效果
    CSS3实现8种Loading效果【二】
    Delphi面向对象的编程思想
    delphi 格式转换
    FindWindow和FindWindowEx函数使用
    delphi TStringList 用法详解
    ExtractFileDir 与 ExtractFilePath 的区别
    C++模板与群体数据
    C++多态性
  • 原文地址:https://www.cnblogs.com/gchlcc/p/5582261.html
Copyright © 2011-2022 走看看