Apple开发_使用自定义的提示音

1、要求

  • 1)使用自定义的提示音(时间必须小于30秒音乐文件 => . caf、.wav 文件)
  • 2)这种方式需要读取工程里的音频文件(.caf、.wav)格式的,然后添加到系统提示音里面,得到 soundID ,
  • 3)然后调用AudioServicesPlaySystemSound(soundID) 即可

2、最常见了三种方式:

  • 1)声音提示(我觉得这个应该是用的最多了吧,谁也不愿意每次收到微信消息都振动吧,😄)
  • 2)振动提示(静音状态)
  • 3)声音加振动(感觉很少用到哈)

3、代码实现

  • 3.1 AudioServicesManager.h

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    typedef NS_ENUM(NSInteger) {
        AudioOnly = 1,  // 声音提示
        VibrateOnly,    // 振动提示
        AudioAndVibrate // 声音&振动
    } AudioServicesType;
    
    @interface AudioServicesManager : NSObject
    
    @property (nonatomic ,assign) AudioServicesType audioServicesType;
    
    + (instancetype)sharedManager;
    
    - (void)play;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
  • 3.2 AudioServicesManager.m

    #import "AudioServicesManager.h"
    
    #import <AudioToolbox/AudioToolbox.h>
    
    NSString *const kFileUrl = @"36_爱给网_aigei_com.wav";
    
    @interface AudioServicesManager()
    
    @property (nonatomic ,assign) SystemSoundID soundID;
    
    @end
    
    @implementation AudioServicesManager
    
    + (instancetype)sharedManager {
        static AudioServicesManager *manager = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            manager = [[AudioServicesManager alloc]init];
        });
        return manager;
    }
    
    - (instancetype)init {
        self = [super init];
        if (self) {
            NSString *audioFile = [[NSBundle mainBundle] pathForResource:kFileUrl ofType:nil];
            NSURL *fileUrl = [NSURL fileURLWithPath:audioFile];
            AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &_soundID);
        }
        return self;
    }
    
    
    #pragma mark - play
    - (void)play {
        switch (_audioServicesType) {
            case AudioOnly:
                [self audioOnly];
                break;
            case VibrateOnly:
                [self vibrateOnly];
                break;
            case AudioAndVibrate:
                [self audioAndVibrate];
                break;
                
            default:
                break;
        }
    }
    
    - (void)audioOnly {
    
    #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
        AudioServicesPlaySystemSound(_soundID);
    #else
        AudioServicesPlaySystemSoundWithCompletion(_soundID, nil);
    #endif
    }
    
    - (void)vibrateOnly {
    #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    #else
        AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil);
    #endif
    }
    
    - (void)audioAndVibrate {
    #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
        AudioServicesPlayAlertSound(_soundID);
    #else
        AudioServicesPlayAlertSoundWithCompletion(_soundID, nil);
    #endif
    }
    
    #pragma mark - dealloc
    - (void)dealloc {
        AudioServicesRemoveSystemSoundCompletion(_soundID);
        AudioServicesDisposeSystemSoundID(_soundID);
    }
    @end
    
    
  • 3.3 使用

    AudioServicesManager *manager = [AudioServicesManager sharedManager];
    // 设置提示类型
    manager.audioServicesType = AudioOnly;
    // 触发提示
    [manager play];
    
posted @ 2022-11-09 11:10  CH520  阅读(43)  评论(0编辑  收藏  举报