Show custom tone as notification tone that saved from document directory iOS swift
Solution:
If you want to show the notification with custom tone that you had saved in document directory then follow the steps to play the tone at notification time.But it had difficult so you just save the audio in libraryDirectory and show.
The below code is used for write the file in the librarydirectory with the folder named sound.You must have to create a directory to the library and save the file
let fileManager = FileManager.default
let documentDirectoryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! as NSURL
var newUrl:NSURL!
let newDir = documentDirectoryURL.appendingPathComponent("Sounds")?.path
do{
try fileManager.createDirectory(atPath: newDir!,withIntermediateDirectories: true, attributes: nil)
newUrl = documentDirectoryURL.appendingPathComponent("Sounds") as! NSURL
} catch {
print("Error: \(error.localizedDescription)")
}
Then export the audio and save it using below code.
let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport?.outputFileType = AVFileTypeAppleM4A
assetExport?.outputURL = mergeAudioFiles as URL
assetExport?.exportAsynchronously(completionHandler:
{
switch assetExport!.status
{
case AVAssetExportSessionStatus.failed:
print("failed \(assetExport?.error)")
case AVAssetExportSessionStatus.cancelled:
print("cancelled \(assetExport?.error)")
case AVAssetExportSessionStatus.unknown:
print("unknown\(assetExport?.error)")
case AVAssetExportSessionStatus.waiting:
print("waiting\(assetExport?.error)")
case AVAssetExportSessionStatus.exporting:
print("exporting\(assetExport?.error)")
default:
print("exporting success")
}
})
The below code is in notification generation time you can use the customized tone.
let content = UNMutableNotificationContent()
content.sound = UNNotificationSound(named: sound!)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(delay),
repeats: false)
let request = UNNotificationRequest(identifier: reminderId, content: content,
trigger: trigger)
notifiCenter.add(request, withCompletionHandler:{ (error) in
if (error != nil) {
print("Something went wrong: %@",error)
}else {
print("All good!: %@", request);
}
If you want to show the notification with custom tone that you had saved in document directory then follow the steps to play the tone at notification time.But it had difficult so you just save the audio in libraryDirectory and show.
The below code is used for write the file in the librarydirectory with the folder named sound.You must have to create a directory to the library and save the file
let fileManager = FileManager.default
let documentDirectoryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first! as NSURL
var newUrl:NSURL!
let newDir = documentDirectoryURL.appendingPathComponent("Sounds")?.path
do{
try fileManager.createDirectory(atPath: newDir!,withIntermediateDirectories: true, attributes: nil)
newUrl = documentDirectoryURL.appendingPathComponent("Sounds") as! NSURL
} catch {
print("Error: \(error.localizedDescription)")
}
Then export the audio and save it using below code.
let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport?.outputFileType = AVFileTypeAppleM4A
assetExport?.outputURL = mergeAudioFiles as URL
assetExport?.exportAsynchronously(completionHandler:
{
switch assetExport!.status
{
case AVAssetExportSessionStatus.failed:
print("failed \(assetExport?.error)")
case AVAssetExportSessionStatus.cancelled:
print("cancelled \(assetExport?.error)")
case AVAssetExportSessionStatus.unknown:
print("unknown\(assetExport?.error)")
case AVAssetExportSessionStatus.waiting:
print("waiting\(assetExport?.error)")
case AVAssetExportSessionStatus.exporting:
print("exporting\(assetExport?.error)")
default:
print("exporting success")
}
})
The below code is in notification generation time you can use the customized tone.
let content = UNMutableNotificationContent()
content.sound = UNNotificationSound(named: sound!)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(delay),
repeats: false)
let request = UNNotificationRequest(identifier: reminderId, content: content,
trigger: trigger)
notifiCenter.add(request, withCompletionHandler:{ (error) in
if (error != nil) {
print("Something went wrong: %@",error)
}else {
print("All good!: %@", request);
}
Comments
Post a Comment