ios - audioPlayer.isPlaying 语句上的线程 1 错误

标签 ios swift if-statement avaudioplayer

当我的 audioPlayer 的 viewDidLoad 时,我正在尝试执行 if 语句。基本上就是说,如果 audioPlayer 正在播放,则暂停它。像这样:

override func viewDidLoad()
    {
        super.viewDidLoad()

        if audioPlayer.isPlaying {
            audioPlayer.pause()
        }
}

但是,在第 5 行 (开始 if 语句),我得到了错误

Thread 1: EXC_BAD_ACCESS (code=1, adress=0x48)

我该如何解决这个问题?谢谢您的帮助!

最佳答案

你不能在这里使用默认的 NSObject 初始值设定项 - AVAudioPlayer 不会覆盖它,所以它不会设置 isPlaying属性(property)(或其任何其他属性(property))。您必须使用它实际实现的初始化器之一:

/* For all of these init calls, if a return value of nil is given you can check outError to see what the problem was.
 If not nil, then the object is usable for playing
*/

/* all data must be in the form of an audio file understood by CoreAudio */
public init(contentsOf url: URL) throws

public init(data: Data) throws


/* The file type hint is a constant defined in AVMediaFormat.h whose value is a UTI for a file format. e.g. AVFileTypeAIFF. */
/* Sometimes the type of a file cannot be determined from the data, or it is actually corrupt. The file type hint tells the parser what kind of data to look for so that files which are not self identifying or possibly even corrupt can be successfully parsed. */
@available(iOS 7.0, *)
public init(contentsOf url: URL, fileTypeHint utiString: String?) throws

@available(iOS 7.0, *)
public init(data: Data, fileTypeHint utiString: String?) throws

关于ios - audioPlayer.isPlaying 语句上的线程 1 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44578842/

相关文章:

ios - iPad 视网膜开发

iphone - UITableViewCell 背景图片 - iPad 开发

swift - 如何正确恢复应用内购买?

Swift Firebase 查询 child 的 child

python - 有什么办法可以减少 if 语句的数量吗?

php - 如何减少 PHP 中 if-else 语句的数量?

iphone - 从NSManagedObjects数组中查找属于某个关系的所有对象

ios - 以 KB 为单位减小 Flash 文件大小?

Swift 获取今天之前五年的日期

javascript - 如何检查div包含哪个背景图像?