本文介绍: 一 概述音效文件存放及获取播放音效简单示例对SoundID进行功能抽取抽取播放音效工具类用于播放各种音效销毁音效二 音效文件存放及获取2.1 将音效文件夹放进项目目录下2.2 检查TARGETS—>Build Phases—>Copy Bundle Resources,是否Copy进去2.3 获取音效文件的路径 NSURL *url=[[NSBundle mainBundle]URLForResource:@”raw/buyao.aac” withExtension
一 概述
二 音效文件存放及获取
2.1 将音效文件夹放进项目目录下
2.2 检查TARGETS—>Build Phases—>Copy Bundle Resources,是否Copy进去
2.3 获取音效文件的路径
NSURL *url=[[NSBundle mainBundle]URLForResource:@"raw/buyao.aac" withExtension:nil];
三 播放音效简单示例(点击屏幕播放)
3.1 过程
3.2 代码
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1-创建URL
NSURL *url=[[NSBundle mainBundle]URLForResource:@"raw/buyao.aac" withExtension:nil];
//2-创建音效ID
SystemSoundID soundID;
AudioServicesCreateSystemSoundID(CFBridgingRetain(url), &soundID);
//3-播放音效(本地音效)
AudioServicesPlayAlertSound(soudID);
}
@end
3.3 效果
四 对SoundID进行功能抽取
4.1 说明
4.2 代码
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property(nonatomic,assign) SystemSoundID soundID;
@end
@implementation ViewController
-(SystemSoundID)soundID
{
if (!_soundID) {
//1-创建URL
NSURL *url=[[NSBundle mainBundle]URLForResource:@"raw/buyao.aac" withExtension:nil];
//2-创建音效ID
AudioServicesCreateSystemSoundID(CFBridgingRetain(url), &_soundID);
}
return _soundID;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//3-播放音效
AudioServicesPlayAlertSound(self.soundID);
}
@end
五 抽取播放音效工具类用于播放各种音效
5.1 抽取AudioTool工具类
AudioTool.h
#import <Foundation/Foundation.h>
@interface AudioTool : NSObject
//播放音效,需要传入需要播放的文件名称
+(void)playAudioWithFilename:(NSString *)fileName;
@end
AudioTool.m
#import "AudioTool.h"
#import <AVFoundation/AVFoundation.h>
@implementation AudioTool
+(void)playAudioWithFilename:(NSString *)fileName
{
//1-创建URL
NSURL *url=[[NSBundle mainBundle]URLForResource:fileName withExtension:nil];
//2-创建音效ID
SystemSoundID soundID;
AudioServicesCreateSystemSoundID(CFBridgingRetain(url), &soundID);
//3-播放本地音效
AudioServicesPlaySystemSound(soundID);
}
@end
用AudioTool播放音效
#import "ViewController.h"
#import "AudioTool.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[AudioTool playAudioWithFilename:@"raw/buyao.aac"];
}
@end
5.2 AudioTool中对soundID优化
说明
代码
#import "AudioTool.h"
#import <AVFoundation/AVFoundation.h>
@implementation AudioTool
static NSMutableDictionary *_soundIDs;
+(void)initialize
{
if (!_soundIDs) {
_soundIDs=[NSMutableDictionary dictionary];
}
}
+(void)playAudioWithFilename:(NSString *)fileName
{
//1-判断文件名是否为nil
if(fileName==nil)return;
//2-从字典中取出音效ID
SystemSoundID soundID=[_soundIDs[fileName] unsignedIntValue];
//3-判断音效ID是否为nil
if(!soundID)
{
NSLog(@"创建新的soundID");
//如果音效ID为nil,根据文件名称加载音效URL
NSURL *url=[[NSBundle mainBundle]URLForResource:fileName withExtension:nil];
//判断url是否为nil
if (!url) return;
//创建音效ID
AudioServicesCreateSystemSoundID(CFBridgingRetain(url), &soundID);
//将音效ID添加到字典中
_soundIDs[fileName]=@(soundID);
}
//4-播放音效
AudioServicesPlaySystemSound(soundID);
}
@end
5.3 播放随机音效(ViewController.m)
#import "ViewController.h"
#import "AudioTool.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//随机播放
NSString *filename=[NSString stringWithFormat:@"raw/m_%02d.wav",arc4random_uniform(14)+3];
[AudioTool playAudioWithFilename:filename];
}
@end
六 销毁音效
6.1 说明
- 当接收到内存警告时销毁音效
- AudioTool中添加disposeAudioWithFilename方法通过文件名销毁音效
- ViewController中didReceiveMemoryWarning方法中调用
6.2 AudioTool
AudioTool.h
@interface AudioTool : NSObject
//播放音效,需要传入需要播放的文件名称
+(void)playAudioWithFilename:(NSString *)fileName;
//销毁音效
+(void)disposeAudioWithFilename:(NSString *)fileName;
@end
AudioTool.m
//销毁音效
+(void)disposeAudioWithFilename:(NSString *)fileName
{
//1-判断文件名是否为nil
if (fileName==nil) return;
//2-从字典中取出音效ID
SystemSoundID soundID=[_soundIDs[fileName] unsignedIntValue];
if (soundID) {
//3-销毁音效ID
AudioServicesDisposeSystemSoundID(soundID);
//4-从字典中移除已经销毁的音效ID
[_soundIDs removeObjectForKey:fileName];
}
}
6.3 ViewController中调用
//接受到内存警告
-(void)didReceiveMemoryWarning
{
[AudioTool disposeAudioWithFilename:@"raw/buyao.wav"];
}
原文地址:https://blog.csdn.net/Calvin_zhou/article/details/124162814
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_39000.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。