【发布时间】:2019-04-15 11:39:19
【问题描述】:
在我的应用中,用户最多可以录制 25 秒的音频。
我将文档目录中的音频保存为“1.m4a”,每当用户再次录制时,它都会替换“1.m4a”中的原始文件。
我想播放“1.m4a”作为本地通知的声音。我发现消息来源说这是不可能的,但也有一些消息来源说它可能存储在 Library/Sounds 目录中。这是什么意思?谁能教我如何做到这一点!
我的音频录制功能运行良好,并且我有一个按钮可以在单击时播放录制的音频。
录制音频:
@IBAction func recordAction(_ sender: UIButton) {
counter = 0
startCounter()
if (audioRecorder == nil) {
audioReadyLabel.isHidden = true
counterLabelOutlet.isHidden = false
numberOfRecords = 1
let fileName = getDirectory().appendingPathComponent("\(numberOfRecords).m4a")
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 12000, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]
do {
audioRecorder = try AVAudioRecorder(url: fileName, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
stopOutlet.isHidden = false
stopLabelOutlet.isHidden = false
recordOutlet.isHidden = true
recordLabelOutlet.isHidden = true
}
catch {
displayAlert(title: "Oops", message: "Recording failed")
}
}
else {
stopOutlet.isHidden = true
stopLabelOutlet.isHidden = true
audioRecorder.stop()
//what does the below mean ***
audioRecorder = nil
counterTimer.invalidate()
}
}
播放音频:
func playPeptalk() {
let path = getDirectory().appendingPathComponent("\(numberOfRecords).m4a")
do {
audioPlayer = try AVAudioPlayer(contentsOf: path)
audioPlayer.play()
}
catch {
}
}
本地通知:
let content = UNMutableNotificationContent()
content.title = "Rise & Shine"
content.subtitle = "Let's start the day!"
content.sound = UNNotificationSound(named: UNNotificationSoundName("1.m4a"))
【问题讨论】:
标签: swift avaudiorecorder localnotification