1.通用短音效ID的获取
#import <Foundation/Foundation.h>

@interface MJAudioTool : NSObject

/**
* 播放音效
*
* @param filename 音效文件名
*/
+ (void)playSound:(NSString *)filename; /**
* 销毁音效
*
* @param filename 音效文件名
*/
+ (void)disposeSound:(NSString *)filename;
@end
#import "MJAudioTool.h"
#import <AVFoundation/AVFoundation.h> @implementation MJAudioTool // 字典: filename作为key, soundID作为value
// 存放所有的音频ID
static NSMutableDictionary *_soundIDDict;
+ (void)initialize
{
_soundIDDict = [NSMutableDictionary dictionary];
} + (void)playSound:(NSString *)filename
{
if (!filename) return; // 1.从字典中取出soundID
SystemSoundID soundID = [_soundIDDict[filename] unsignedLongValue];
if (!soundID) { // 创建
// 加载音效文件
NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:nil]; if (!url) return; // 创建音效ID
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID); // 放入字典
_soundIDDict[filename] = @(soundID);
} // 2.播放
AudioServicesPlaySystemSound(soundID);
} + (void)disposeSound:(NSString *)filename
{
if (!filename) return; SystemSoundID soundID = [_soundIDDict[filename] unsignedLongValue];
if (soundID) {
// 销毁音效ID
AudioServicesDisposeSystemSoundID(soundID); // 从字典中移除
[_soundIDDict removeObjectForKey:filename];
}
} @end

2.通用方法的使用

[MJAudioTool playSound:@"buyao.wav"];

ios 短音效的使用的更多相关文章

  1. 整理:iOS 短信与电话事件的获取

    整理:iOS 短信与电话事件的获取   background information: Core Telephony iOS 4.0 的官方 API 裡頭,多了一個叫做 Core Telephony  ...

  2. cocos2dx 3.x simpleAudioEngine 长音效被众多短音效打断问题

    假设先play长音效a,然后在a播放过程中反复执行:play短音效b,stop b,play b,... 则若a足够长,就会被b打断.而长音效被打断是最不可接受的. a之所以会被打断,推测原因是sim ...

  3. 美拍、秒拍中安卓、IOS短视频拍摄的一些关键技术

    在发布<EasyDarwin开发出类似于美拍.秒拍的短视频拍摄SDK:EasyVideoRecorder>后,今天有人问到像美拍.秒拍这些短视频拍摄是怎么做到的,包括其中的一些功能,例如: ...

  4. 如何实现 iOS 短视频跨页面的无痕续播?

    在一切皆可视频化的今天,短视频内容作为移动端产品新的促活点,受到了越来越多的重视与投入.盒马在秒播.卡顿率.播放成功率等基础优化之外,在用户使用体验上引入了无痕续播能力,提升用户观看视频内容的延续性. ...

  5. ios短信和电话--参考

    调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 调 ...

  6. 从350ms到80ms,揭秘阿里工程师 iOS 短视频优化方案

    内容作为 App 产品新的促活点,受到了越来越多的重视与投入,短视频则是增加用户粘性.增加用户停留时长的一把利器.短视频的内容与体验直接关系到用户是否愿意长时停留,盒马也提出全链路内容视频化的规划,以 ...

  7. iOS 短视频开发总结二 AVCaptureSession

    开始录制短视频 录制完成 根据项目需求,对视频URL进行处理 基本原理和方法已经完成,具体细节根据项目实际需求再处理

  8. iOS开发,音效的播放简单实现以及音效播放的简单封装

    一.音效的播放简单实现 二.音效播放的封装 -- 封装思路:将生成的SystemSoundID存放到字典中,每次播放的时候从字典中取出对应的SystemSoundID,没有的话再创建 头文件中定义类方 ...

  9. iOS 短信分享 邮件分享

    本地调用短信分享. #import "shareViewController.h" @interface shareViewController (){ UIAlertView * ...

随机推荐

  1. P1136 迎接仪式 (动态规划)

    题目描述 LHX教主要来X市指导OI学习工作了.为了迎接教主,在一条道路旁,一群Orz教主er穿着文化衫站在道路两旁迎接教主,每件文化衫上都印着大字.一旁的Orzer依次摆出“欢迎欢迎欢迎欢迎……”的 ...

  2. PHP简单留言板

    <?php header("Content-Type:text/html;charset=utf8"); $filename = "message.txt" ...

  3. 【HDOJ5978】To begin or not to begin(概率)

    题意:有k个黑球和1个红球,两个轮流抽,抽到红球算赢,问先手赢的概率大还是后手大还是相等 k<=1e5 思路:手算前几项概率 大胆猜想 #include<cstdio> #inclu ...

  4. Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp

    D - MUH and Cube Walls Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  5. PAT (Advanced Level) 1089. Insert or Merge (25)

    简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  6. codevs——1842 递归第一次

    1842 递归第一次  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解       题目描述 Description 同学们在做题时常遇到这种函数 f( ...

  7. Effective Java P2 Creating and Destroying Objects

    This chapter concerns creating and destorying objects : when and how to create them, when and how to ...

  8. iOS Application Security

    文章分A,B,C,D 4个部分. A) iOS Application Security 下面介绍iOS应用安全,如何分析和动态修改app. 1)iOS Application security Pa ...

  9. Mac--安装kubernetes并运行echoserver

    安装minikube curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.15.0/minikube-darwi ...

  10. 带您了解Oracle层次查询

    http://database.51cto.com/art/201010/231539.htm Oracle层次查询(connect by )是结构化查询中用到的,下面就为您介绍Oracle层次查询的 ...