Ios AVAudioPlayer第一次未正确播放音频
我正在做一个项目,必须播放用户自己录制的音频。当应用程序第一次启动并且用户录制了一点音频时,当尝试播放时,AVAudioPlayer似乎在播放,但立即停止。如果用户尝试录制另一段音频,AVAudioPlayer可以正常工作 播放和停止音频的代码:Ios AVAudioPlayer第一次未正确播放音频,ios,swift,audio,avaudioplayer,avaudiorecorder,Ios,Swift,Audio,Avaudioplayer,Avaudiorecorder,我正在做一个项目,必须播放用户自己录制的音频。当应用程序第一次启动并且用户录制了一点音频时,当尝试播放时,AVAudioPlayer似乎在播放,但立即停止。如果用户尝试录制另一段音频,AVAudioPlayer可以正常工作 播放和停止音频的代码: @objc func playAudio() { if audioPlayer == nil { do{ if FileManager.default.fileExists(atPath: filenam
@objc func playAudio() {
if audioPlayer == nil {
do{
if FileManager.default.fileExists(atPath: filename.path) {
print("File Exists")
}
else {
print("File Doesnt Exists")
}
try audioPlayer = AVAudioPlayer(contentsOf: filename)
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
audioPlayer.play()
print(audioPlayer.isPlaying)
print("Playing Audio")
playStopButton.setImage(#imageLiteral(resourceName: "Stop Button"), for: .normal)
}catch {
//displayAlert(title: "Oops!", message: "There was an error. Try Recording Again")
print(error)
}
}
else {
playStopButton.setImage(#imageLiteral(resourceName: "Play Button"), for: .normal)
audioPlayer.stop()
audioPlayer = nil
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
print("Stopping Cuase Comlete")
playStopButton.setImage(#imageLiteral(resourceName: "Play Button"), for: .normal)
audioPlayer.stop()
audioPlayer = nil
}
}
录音代码
@objc func record() {
if audioRecorder == nil{
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()
//view.backgroundColor = UIColor.red
//recordButton.setTitle("Stop Recording", for: .normal)
setupTimer()
timerDisplay.isHidden = false
shapeLayer.fillColor = UIColor.black.cgColor
}catch{
displayAlert(title: "Oops!", message: "Recording Failed")
}
}
else{
audioRecorder.stop()
timer?.invalidate()
timer = nil
shapeLayer.strokeEnd = 0
audioRecorder=nil;
timerDisplay.isHidden = true
shapeLayer.fillColor = UIColor.red.cgColor
let selected = arr[picker.selectedRow(inComponent: 0)]
UserDefaults.standard.set(selected, forKey: "SelectedTopic")
present(UINavigationController(rootViewController: PlaybackController()), animated: true, completion: nil)
}
}
录音机也有同样的问题。我通过设置播放器的方法解决了这个问题。像这样:
func setupView() {
recordingSession = AVAudioSession.sharedInstance()
do {
try recordingSession.setCategory(.playAndRecord, mode: .default)
try recordingSession.setActive(true)
recordingSession.requestRecordPermission() { [unowned self] allowed in
DispatchQueue.main.async {
if allowed {
self.loadRecordingUI()
} else {
// failed to record
}
}
}
} catch {
// failed to record
}
}
然后在我的viewDidLoad.as中调用它:
self.setupView()
录音机也有同样的问题。我通过设置播放器的方法解决了这个问题。像这样:
func setupView() {
recordingSession = AVAudioSession.sharedInstance()
do {
try recordingSession.setCategory(.playAndRecord, mode: .default)
try recordingSession.setActive(true)
recordingSession.requestRecordPermission() { [unowned self] allowed in
DispatchQueue.main.async {
if allowed {
self.loadRecordingUI()
} else {
// failed to record
}
}
}
} catch {
// failed to record
}
}
然后在我的viewDidLoad.as中调用它:
self.setupView()
检查第一次提供的路径是否正确?@iOSGeek yes每次都返回true文件存在我看不出会发生任何错误,只是因为文件名提供不正确,因为第二次工作正常,很难捕获error@iOSGeek不可能是文件名每次我面对同样的问题时文件名都是一样的,您找到解决方案了吗?检查第一次提供的路径是否正确?@iOSGeek yes每次都返回true该文件存在我看不出有任何错误可能是因为文件名提供不正确,因为第二次工作正常,难追error@iOSGeek不可能是文件名每次我面对同一个问题时,文件名都是一样的,你找到解决办法了吗?