在iOS13.1上播放音频时,应用崩溃

编程入门 行业动态 更新时间:2024-10-28 14:25:56
本文介绍了在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上播放音频时,应用崩溃

本文发布于:2023-07-11 11:52:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:音频

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!