我制作的键盘扩展程序使用音频文件在按下按键时播放音频反馈。在某些时候,用户能够将多个音频文件组合成单个音频文件。组合多个音频文件在模拟器中工作,但在设备上不起作用。
func createSound(myNotes: [String], outputFile: String) {
// CMTime struct represents a length of time that is stored as rational number
var startTime: CMTime = kCMTimeZero
// AVMutableComposition creates new composition
let composition: AVMutableComposition = AVMutableComposition()
// AVMutableCompositionTrack - A mutable track in composition that you use to insert, remove, and scale track segments
if let compositionAudioTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid) {
for url in allFilesForCharacters() {
let avAsset: AVURLAsset = AVURLAsset(url: url)
let timeRange: CMTimeRange = CMTimeRangeMake(kCMTimeZero, avAsset.duration)
let audioTrack: AVAssetTrack = avAsset.tracks(withMediaType: AVMediaType.audio)[0]
try! compositionAudioTrack.insertTimeRange(timeRange, of: audioTrack, at: startTime)
startTime = CMTimeAdd(startTime, timeRange.duration)
}
}
let exportPath: String = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].path+"/"+outputFile+".m4a"
try? FileManager.default.removeItem(atPath: exportPath)
if let export: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) {
export.outputURL = URL(fileURLWithPath: exportPath)
export.outputFileType = AVFileType.m4a
export.exportAsynchronously {
if export.status == AVAssetExportSessionStatus.completed {
NSLog("All done");
if let data = try? Data(contentsOf: export.outputURL!) {
let board = UIPasteboard.general
board.setData(data, forPasteboardType: kUTTypeMPEG4Audio as String)
}
}
else {
print(export.error?.localizedDescription ?? "")
}
}
}
}
答案 0 :(得分:1)
所以我意识到应用程序在其设置中有一个允许完全访问开关后能够解决问题。打开后,一切都按预期工作。该应用无法执行其功能,因为该设备阻止其访问设备数据。