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]);
      }
  • 相关阅读:
    索引
    convert 时间转换
    SQL中的case when then else end用法
    SVN安装图解
    dos 命令
    阿里云服务器相关知识
    mvc知识应用
    MVC分页
    mvc系统过滤器
    .net 接口
  • 原文地址:https://www.cnblogs.com/gchlcc/p/5582261.html
Copyright © 2011-2022 走看看