一 概述

二 音效文件存放获取

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), &amp;soundID);
    //3-播放音效(本地音效)
    AudioServicesPlayAlertSound(soudID);
}

@end

3.3 效果

点击屏幕,播放buyao音效

四 对SoundID进行功能抽取

4.1 说明

  • 上例三每次点击都要重新创建SoundID
  • 本地对SoundID进行抽取,没有就创建,有则直接使用SoundID

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), &amp;_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), &amp;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), &amp;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 说明

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进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注