iOS 自定义通知声音

官方文档

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SupportingNotificationsinYourApp.html#//apple_ref/doc/uid/TP40008194-CH4-SW10

重点提示:
1、格式

  • Linear PCM
  • MA4 (IMA/ADPCM)
  • µLaw
  • aLaw
    2、文件必须写在app bundle 或者沙盒Library/Sounds文件夹中
Place custom sound files in your app bundle or in the Library/Sounds folder of your app’s container directory. Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.

3、读取文件直接写名字@"name",不用写

[NSBundle mainBundle] pathForResource:<#(nullable NSString *)#> ofType:<#(nullable NSString *)#>

或者沙盒路径。

附带将自己plist文件存入沙盒中代码

/* 写音频文件  */
- (void)writeMusicData {
    NSString *bundlePath = [PPSDKbundle pathForResource:@"voip_call" ofType:@"caf"];
    NSString *libPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Sounds/"];

    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:libPath]) {
        NSError *error;
        [manager createDirectoryAtPath:libPath withIntermediateDirectories:YES attributes:nil error:&error];
    }

    NSData *data = [NSData dataWithContentsOfFile:bundlePath];
    BOOL flag = [data writeToFile:[libPath stringByAppendingString:@"voip_call.caf"] atomically:YES];
    if (flag) {
        NSLog(@"文件写成功");
    }else{
        NSLog(@"文件写失败");
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容