ios 简单系统提示音

此方法为获取音频文件的相关信息

 NSString *audiopath = [[NSBundle mainBundle]pathForResource:@"9415" ofType:@"wav"];
    NSURL * audioURL = [NSURL fileURLWithPath:audiopath];
    AudioFileID audioFile;
    //打开
    AudioFileOpenURL((__bridge CFURLRef _Nonnull)(audioURL), kAudioFileReadPermission, 0, &audioFile);
    //读取
    UInt32 dictionarySize = 0;
    AudioFileGetPropertyInfo(audioFile, kAudioFilePropertyInfoDictionary, &dictionarySize, 0);
    CFDictionaryRef  dictionary;
    AudioFileGetProperty(audioFile  , kAudioFilePropertyInfoDictionary, &dictionarySize, &dictionary);
    NSDictionary *audioDic = (__bridge NSDictionary *)dictionary;
    for (int i = 0; i < [audioDic allKeys].count; i++) {
        NSString *key =[[audioDic allKeys]objectAtIndex:i];
        NSString *value =[audioDic valueForKey:key];
        NSLog(@"打印数据%@,%@",key,value);
    }
    CFRelease(dictionary);
    AudioFileClose(audioFile);


此方法为添加震动,注意之后手机才有震动,iPad没有震动因此需要判断

  NSString *ddevue = [[UIDevice currentDevice] model];
    if ([ddevue isEqualToString:@"iPhone"]) {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }

添加自定义系统提示音导入的音频文件要低于30s,否则无法使用

    /*
     当手机静音的时候此方法失效
     */

    NSURL *systemSound_url =[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"9418" ofType:@"wav"]];
    //创建ID
    SystemSoundID systemSound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(systemSound_url), &systemSound_id);
    //注册callback
    AudioServicesAddSystemSoundCompletion(systemSound_id, NULL, NULL, SoundFInishedPlaying, NULL);
    //播放系统声音
    AudioServicesPlaySystemSound(systemSound_id);


这里面的SoundFInishedPlaying方法实际是一个C方法

void SoundFInishedPlaying(SystemSoundID sound_id, void *user_data){
    AudioServicesRemoveSystemSoundCompletion(sound_id);
    AudioServicesDisposeSystemSoundID(sound_id);
}

此方法放在的位置如下

@interface ViewController ()

@end
void SoundFInishedPlaying(SystemSoundID sound_id, void *user_data){
    AudioServicesRemoveSystemSoundCompletion(sound_id);
    AudioServicesDisposeSystemSoundID(sound_id);
}
@implementation ViewController

上面的方法在静音模式下是失效的无法提示,我们只需替换最后一句代码AudioServicesPlaySystemSound(systemSound_id);改为  AudioServicesPlayAlertSound(systemSound_id);

这个方法在正常模式下为震动加提示音,在静音模式下为震动



方法中的音频文件自己上网上下载小于30s的提示音就可以使用

猜你喜欢

转载自blog.csdn.net/nopolun_ios/article/details/78737665