【问题标题】:Unable to use recorded clip as sound in local notification无法在本地通知中将录制的剪辑用作声音
【发布时间】: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


    【解决方案1】:

    来自Docs

    音频文件必须已经在用户的设备上才能播放。如果您为通知使用一组预定义的声音,请将音频文件包含在您的应用程序包中。对于所有其他声音,请将音频文件的副本放在应用容器目录的 Library/Sounds 文件夹中。 UNNotificationSound 对象只在这两个位置查找

    【讨论】:

    • 如何将音频文件的副本放在我的应用容器目录的 Library/Sounds 文件夹中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多