在iOS13.1上播放音频时,应用崩溃 [英] App crashes when playing audio on iOS13.1
问题描述
我正在构建一个应用程序,该应用程序使用URL从主捆绑包中运行声音文件.当我在iOS 13上进行测试时,一切都很好.但是,随着新的13.1更新,我在代码行中出现了错误
I am building an app that runs sound files from within the main bundle with a url. When I tested this on iOS 13, everything is fine. But with the new update of 13.1 I am getting an error here on the line of code
backgroundMusicPlayer =尝试AVAudioPlayer(contentsOf:URL(fileURLWithPath:sound!))
backgroundMusicPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))
说:
线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x48
Thread 1: EXC_BAD_ACCESS (code=1, address=0x48
这是我在自定义类中使用的代码,该类在应用启动时运行背景音乐:
Here is the code that I am using in a custom class that runs background music when the app launches:
import Foundation
import AVFoundation
var backgroundMusicPlayer = AVAudioPlayer()
func playBackgroundMusic(filename: String){
let sound = Bundle.main.path(forResource: filename, ofType: "m4a")
do{
try
AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [AVAudioSession.CategoryOptions.mixWithOthers])
backgroundMusicPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))
}catch{
print (error)
}
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.play()
}
这一切在iOS13上的模拟器中都可以正常运行,但是在运行13.1的设备上崩溃似乎是问题所在,但我不确定为什么.在其他屏幕上,按钮正在触发捆绑软件中的音频文件时,也会发生相同的行为.
This all works fine in the simulator on iOS13, but crashes on a device running 13.1 It appears that the url is the issue, but I am not sure why. This same behavior happens on other screens where buttons are triggering audio files from the bundle.
推荐答案
更改此内容:
var backgroundMusicPlayer = AVAudioPlayer()
对此:
var backgroundMusicPlayer : AVAudioPlayer!
这篇关于在iOS13.1上播放音频时,应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!