本地通知中带有路径的声音不起作用

时间:2018-07-02 14:00:22

标签: ios objective-c notifications

我正在从Web下载声音文件,并且我得到的路径在这里/var/mobile/Containers/Data/Application/9A3E2F61-AD0F-42B1-9BDA-81922E630AB6/Library/NoCloud/My_App/SoundTunes/My_Tune.wav

现在,尽管我的持续时间my_tune短于default tune,但我正在尽一切努力设置my_tune进行通知,但每次响起25 seconds时都会发出通知{p

帮我如何设置my_tune到本地通知环的路径

NSURL *urlTuneTime = [[NSFileManager.defaultManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *urlTuneTimeok = [urlTuneTime URLByAppendingPathComponent:@"NoCloud/My_App/SoundTunes/"];
NSURL *urlTuneTimeokk = [urlTuneTimeok URLByAppendingPathComponent:getTuneTime];
NSLog(@"here is the overall got path of time tune%@", urlTuneTimeokk);

content.sound = [UNNotificationSound soundNamed: @"../SoundTunes/My_Tune.wav" ];

//content.sound = [UNNotificationSound soundNamed: @"NoCloud/MyApp/SoundTunes/My_Tune.wav" ];

1 个答案:

答案 0 :(得分:0)

根据soundNamed initializer上的UNNotificationSound文档:

  

此文件必须位于当前可执行文件的主捆绑包中或当前应用容器目录的Library / Sounds目录中。

因此,您必须使用URL将下载的文件保存到应用程序的Library/Sounds目录中:

// save your file here
NSURL *soundsDirectoryUrl = [[[NSFileManager.defaultManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] firstObject] URLByAppendingPathComponent:@"Sounds"];
NSURL *fileUrl = [soundsDirectoryUrl URLByAppendingPathComponent:@"fileName.wav"];

// create UNNotificationSound
content.sound = [UNNotificationSound soundNamed: "fileName.wav" ];