gpt4 book ai didi

swift - 音频在模拟器中播放,但不在设备上播放

转载 作者:行者123 更新时间:2023-11-28 05:42:40 25 4
gpt4 key购买 nike

我正在开发一个播放音频文件的应用程序,并且可以使用 xcode 模拟器 (v10.2) 中的 AVPlayer 实例来播放文件,但是当尝试在我的设备上播放此文件时,音频文件无法播放。

我已阅读对同一问题的回复并进行了检查:

  • 设备上的静音铃声开关未打开
  • 我还没有在 viewDidLoad 中实例化 AVPlayer
  • 代码中音频文件的名称与文件中的名称相同

以下是我设置记录器的代码:

func setUpRecorder(storyItem : StoryItem) {

// Generate unique ID for recording for the StoryItem
let uuid = UUID().uuidString + ".m4a"

let audioFileName = getDocumentDirectory().appendingPathComponent(uuid)

do {
try self.realm.write {
// storyItem.recording = audioFileName.absoluteString
storyItem.recording = uuid
}
} catch {
print("Error saving recording \(error)")
}
self.createStoryTableView.reloadData()

let recordSettings = [AVFormatIDKey : kAudioFormatAppleLossless,
AVEncoderAudioQualityKey : AVAudioQuality.max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey : 2,
AVSampleRateKey : 44100.0 ] as [String : Any]

do {
audioRecorder = try AVAudioRecorder(url: audioFileName, settings: recordSettings)
audioRecorder.delegate = self
audioRecorder.prepareToRecord()
} catch {
print(error)
}
}

播放音频文件的方法在这里:

func setUpPlayer(storyItem : StoryItem) {

    let audioFileName = getDocumentDirectory().appendingPathComponent(storyItem.recording)

getAudioDuration(audioFileName: audioFileName)

if storyItem.recording == "" {

let alert = UIAlertController(title: "There's no recording", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Cancel", style: .default) { (action) in
}
// This is what happens when cancel is pressed

alert.addAction(action)

self.present(alert, animated: true, completion: nil)
} else {

player = AVPlayer(url: audioFileName)
player.volume = 1.0

}
}

如有任何建议,我将不胜感激。难道是使用了唯一标识符(UUID)作为音频文件的名称?

最佳答案

尝试在录制之前配置 AVAudioSession 共享实例。

像这样:

// Configure AVAudioSession
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch {
assertionFailure("Failed to configure `AVAAudioSession`: \(error.localizedDescription)")
}

关于swift - 音频在模拟器中播放,但不在设备上播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56024134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com