ios - 如何修复线程 1:EXC_BAD_ACCESS (code=1, address=0x58) xcode
问题描述
我正在尝试使用按钮播放一些声音文件,但按下按钮会引发此错误 Thread 1: EXC_BAD_ACCESS (code=1, address=0x58) on line
audioPlayer.play()
我一直在寻找可能的解决方案,但找不到与该错误相关的任何内容,我的代码功能在打印之前运行良好,这是我的完整代码。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var track: String? = nil
var audioPlayer = AVAudioPlayer()
@IBAction func heavyButton(_ sender: Any) {
track = "H"
print("heavy machine gun \(track!)")
reproducirAudio(audio: track!)
audioPlayer.play()
}
func reproducirAudio(audio: String) {
do {
print("entro a la funcion de reproducir")
audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: audio, ofType: "mp3")!))
audioPlayer.prepareToPlay()
} catch {
print(error)
}
}
}
解决方案
我找到了一个解决方案,这是针对 iOS13
import UIKit
import AVFoundation
class ViewController: UIViewController {
var track: String? = nil
var audioPlayer: AVAudioPlayer?
@IBAction func heavyButton(_ sender: Any) {
track = "H"
print("heavy machine gun \(track!)")
reproducirAudio(audio: track!)
}
func reproducirAudio(audio: String) {
let path = Bundle.main.path(forResource: "\(audio).mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer?.play()
} catch {
// couldn't load file :(
}
}
推荐阅读
- java - 如何将 java 代理附加到正在运行的 spring-boot 应用程序
- javascript - 未处理的拒绝(TypeError):调度不是在动作创建者中获得此错误的函数
- api - Newtonsoft.Json 在 RestEase 中抛出异常
- firebase - 如何将 arduino Uno 与 gsm/gprs SIM900 屏蔽连接到 Firebase?
- string - 检查来自 invoke-webrequest 的字符串内容
- c# - FluentValidation:将错误消息分配给验证逻辑之外的另一个变量
- php - 如何加载 PHP 扩展?
- python - 已训练的 tensorflow 模型移至其他目录无法加载
- google-chrome - 该网站如何访问 Chrome 菜单
- javascript - 如何以角度从表单数组中读取内容?