gpt4 book ai didi

ios - 单击时avaudioPlayer音频未打开(swift4)

转载 作者:行者123 更新时间:2023-11-30 10:53:40 25 4
gpt4 key购买 nike

来自 AVaudioPlayer 的声音文件未打开,因此我录制的声音都没有在桌面 View 上播放。我可以保存到索引并在表格 View 上看到它。但是当我单击表格 View 单元格时,我在日志文件中看到此错误消息。

2019-01-14 17:59:24.727900-0500 audioForAntoehr[6991:65804] [AXMediaCommon] Unable to look up screen scale

下面的代码是我的源代码。我只使用一个 View Controller 。

import UIKit;import AVFoundation

class ViewController: UIViewController, AVAudioRecorderDelegate, UITableViewDataSource, UITableViewDelegate {
var recordingSessioin: AVAudioSession!
var audioRecorder: AVAudioRecorder!
var aduioPlayer : AVAudioPlayer!

@IBOutlet var mytableVie3w : UITableView!

var numberOfRecords = 0
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfRecords
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for : indexPath)
cell.textLabel?.text = String(indexPath.row + 1)
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let path = getD().appendingPathComponent("\(indexPath.row + 1).m4a")

do {
aduioPlayer = try AVAudioPlayer(contentsOf: path)
aduioPlayer.play()
}
catch {

}}
@IBOutlet var buttonL:UIButton!
@IBAction func record(_ sender: Any){
if audioRecorder == nil {
numberOfRecords += 1
let fileName = getD().appendingPathComponent("\(numberOfRecords).m4a")
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey : 12000, AVNumberOfChannelsKey : 1, AVEncoderBitRateKey : AVAudioQuality.high.rawValue]


do {
audioRecorder = try AVAudioRecorder(url: fileName, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
buttonL.setTitle("stop", for: .normal)

}

catch {
dsiaplery(title: "Ups", message: "it failed")
}
}
else {
audioRecorder.stop()
audioRecorder = nil
buttonL.setTitle("Stat", for: .normal)
UserDefaults.standard.set(numberOfRecords, forKey: "myN")
}



}


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

recordingSessioin = AVAudioSession.sharedInstance()

AVAudioSession.sharedInstance().requestRecordPermission { (hasPermission) in
if hasPermission {
print("accepted")
}
}
if let number:Int = UserDefaults.standard.object(forKey: "myN") as? Int
{
numberOfRecords = number
}
}

func getD() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documenterd = paths[0]
return documenterd
}

func dsiaplery(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "dismiss", style: .default, handler: nil))
present(alert, animated: true, completion: nil)


}
}

最佳答案

编辑:这里有更好的答案 How to play a sound using swift

当您需要在 iOS 中播放一些音频时,您需要向播放器指定您的共享 session 。iOS 环境(OS)需要被通知才能知道想要什么。

在调用audioPlayer.play()之前,您需要将 session 设置为当前的audioPlayer,您当前的方法应该如下所示。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let path = getD().appendingPathComponent("\(indexPath.row + 1).m4a")
do {
// current class instance
if (self.playSession == nil) {
self.playSession = AVAudioSession.sharedInstance()
try self.playSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)
}
aduioPlayer = try AVAudioPlayer(contentsOf: path)
aduioPlayer.play()
} catch {}

}

iOS Audio Session

关于ios - 单击时avaudioPlayer音频未打开(swift4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54190496/

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